Skip to content

Commit

Permalink
Use reference counting for completion items
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Jun 7, 2019
1 parent 3e41fa0 commit fd0edad
Show file tree
Hide file tree
Showing 23 changed files with 111 additions and 141 deletions.
7 changes: 3 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jsonrpc = { path = "jsonrpc" }
jsonrpc-derive = { path = "jsonrpc_derive" }
lazy_static = "1.3.0"
log = "0.4.6"
lsp-types = { git = "https://github.com/latex-lsp/lsp-types", rev = "8ce88a90c6753cf10d4b97d8cd6c9b8bb663c9d1", features = ["proposed"] }
lsp-types = { git = "https://github.com/latex-lsp/lsp-types", rev = "eb34c6ebd1e327946e62a9bbc3835a850fd260b3", features = ["proposed"] }
nom = "5.0.0-beta2"
path-clean = "0.1.0"
regex = "1.1.6"
Expand Down
11 changes: 5 additions & 6 deletions src/completion/bibtex/entry_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,30 @@ use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::sync::Arc;

#[derive(Debug, PartialEq, Clone)]
pub struct BibtexEntryTypeCompletionProvider {
items: Vec<CompletionItem>,
items: Vec<Arc<CompletionItem>>,
}

impl BibtexEntryTypeCompletionProvider {
pub fn new() -> Self {
let items = BIBTEX_ENTRY_TYPES
.iter()
.map(factory::create_entry_type)
.map(Arc::new)
.collect();
Self { items }
}
}

impl FeatureProvider for BibtexEntryTypeCompletionProvider {
type Params = CompletionParams;
type Output = Vec<CompletionItem>;
type Output = Vec<Arc<CompletionItem>>;

#[boxed]
async fn execute<'a>(
&'a self,
request: &'a FeatureRequest<CompletionParams>,
) -> Vec<CompletionItem> {
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
if let SyntaxTree::Bibtex(tree) = &request.document.tree {
for declaration in &tree.root.children {
match declaration {
Expand Down
11 changes: 5 additions & 6 deletions src/completion/bibtex/field_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,30 @@ use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::sync::Arc;

#[derive(Debug, PartialEq, Clone)]
pub struct BibtexFieldNameCompletionProvider {
items: Vec<CompletionItem>,
items: Vec<Arc<CompletionItem>>,
}

impl BibtexFieldNameCompletionProvider {
pub fn new() -> Self {
let items = BIBTEX_FIELDS
.iter()
.map(|field| factory::create_field_name(field))
.map(Arc::new)
.collect();
Self { items }
}
}

impl FeatureProvider for BibtexFieldNameCompletionProvider {
type Params = CompletionParams;
type Output = Vec<CompletionItem>;
type Output = Vec<Arc<CompletionItem>>;

#[boxed]
async fn execute<'a>(
&'a self,
request: &'a FeatureRequest<CompletionParams>,
) -> Vec<CompletionItem> {
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
if let SyntaxTree::Bibtex(tree) = &request.document.tree {
match tree.find(request.params.position).last() {
Some(BibtexNode::Field(field)) => {
Expand Down
11 changes: 5 additions & 6 deletions src/completion/bibtex/kernel_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use crate::syntax::SyntaxTree;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
use std::sync::Arc;

#[derive(Debug, PartialEq, Clone)]
pub struct BibtexKernelCommandCompletionProvider {
items: Vec<CompletionItem>,
items: Vec<Arc<CompletionItem>>,
}

impl BibtexKernelCommandCompletionProvider {
Expand All @@ -20,20 +21,18 @@ impl BibtexKernelCommandCompletionProvider {
.iter()
.map(|command| Cow::from(*command))
.map(|command| factory::create_command(command, &LatexComponentId::Kernel))
.map(Arc::new)
.collect();
Self { items }
}
}

impl FeatureProvider for BibtexKernelCommandCompletionProvider {
type Params = CompletionParams;
type Output = Vec<CompletionItem>;
type Output = Vec<Arc<CompletionItem>>;

#[boxed]
async fn execute<'a>(
&'a self,
request: &'a FeatureRequest<CompletionParams>,
) -> Vec<CompletionItem> {
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
if let SyntaxTree::Bibtex(tree) = &request.document.tree {
if let Some(BibtexNode::Command(command)) = tree.find(request.params.position).last() {
if command.token.range().contains(request.params.position)
Expand Down
12 changes: 5 additions & 7 deletions src/completion/latex/argument_symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ use crate::data::symbols::DATABASE;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::sync::Arc;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct LatexArgumentSymbolCompletionProvider;

impl FeatureProvider for LatexArgumentSymbolCompletionProvider {
type Params = CompletionParams;
type Output = Vec<CompletionItem>;
type Output = Vec<Arc<CompletionItem>>;

#[boxed]
async fn execute<'a>(
&'a self,
request: &'a FeatureRequest<CompletionParams>,
) -> Vec<CompletionItem> {
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
let mut items = Vec::new();
for group in &DATABASE.arguments {
let command = format!("\\{}", group.command);
Expand All @@ -29,10 +27,10 @@ impl FeatureProvider for LatexArgumentSymbolCompletionProvider {
async move |_| {
let mut items = Vec::new();
for symbol in &group.arguments {
items.push(factory::create_argument_symbol(
items.push(Arc::new(factory::create_argument_symbol(
&symbol.argument,
&symbol.image,
));
)));
}
items
},
Expand Down
10 changes: 4 additions & 6 deletions src/completion/latex/begin_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@ use crate::feature::{FeatureProvider, FeatureRequest};
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
use std::sync::Arc;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct LatexBeginCommandCompletionProvider;

impl FeatureProvider for LatexBeginCommandCompletionProvider {
type Params = CompletionParams;
type Output = Vec<CompletionItem>;
type Output = Vec<Arc<CompletionItem>>;

#[boxed]
async fn execute<'a>(
&'a self,
request: &'a FeatureRequest<CompletionParams>,
) -> Vec<CompletionItem> {
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
LatexCombinators::command(request, async move |_| {
let snippet = factory::create_snippet(
Cow::from("begin"),
&LatexComponentId::Kernel,
Cow::from("begin{$1}\n\t$0\n\\end{$1}"),
);
vec![snippet]
vec![Arc::new(snippet)]
})
.await
}
Expand Down
10 changes: 4 additions & 6 deletions src/completion/latex/citation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@ use crate::syntax::latex::CITATION_COMMANDS;
use crate::syntax::SyntaxTree;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::sync::Arc;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct LatexCitationCompletionProvider;

impl FeatureProvider for LatexCitationCompletionProvider {
type Params = CompletionParams;
type Output = Vec<CompletionItem>;
type Output = Vec<Arc<CompletionItem>>;

#[boxed]
async fn execute<'a>(
&'a self,
request: &'a FeatureRequest<CompletionParams>,
) -> Vec<CompletionItem> {
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
LatexCombinators::argument(request, CITATION_COMMANDS, 0, async move |_| {
let mut items = Vec::new();
for document in &request.related_documents {
if let SyntaxTree::Bibtex(tree) = &document.tree {
for entry in &tree.entries() {
if !entry.is_comment() {
if let Some(key) = &entry.key {
items.push(factory::create_citation(entry, key.text()));
items.push(Arc::new(factory::create_citation(entry, key.text())));
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/completion/latex/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ use crate::feature::{FeatureProvider, FeatureRequest};
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
use std::sync::Arc;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct LatexColorCompletionProvider;

impl FeatureProvider for LatexColorCompletionProvider {
type Params = CompletionParams;
type Output = Vec<CompletionItem>;
type Output = Vec<Arc<CompletionItem>>;

#[boxed]
async fn execute<'a>(
&'a self,
request: &'a FeatureRequest<CompletionParams>,
) -> Vec<CompletionItem> {
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
LatexCombinators::argument(request, &COLOR_COMMANDS, 0, async move |_| {
COLOR_NAMES
.iter()
.map(|name| factory::create_color(Cow::from(*name)))
.map(Arc::new)
.collect()
})
.await
Expand Down
15 changes: 7 additions & 8 deletions src/completion/latex/color_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::feature::{FeatureProvider, FeatureRequest};
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
use std::sync::Arc;

#[derive(Debug, PartialEq, Clone)]
pub struct LatexColorModelCompletionProvider {
items: Vec<CompletionItem>,
items: Vec<Arc<CompletionItem>>,
}

impl LatexColorModelCompletionProvider {
Expand All @@ -16,14 +17,15 @@ impl LatexColorModelCompletionProvider {
.iter()
.map(|name| Cow::from(*name))
.map(factory::create_color_model)
.map(Arc::new)
.collect();
Self { items }
}

async fn execute_define_color<'a>(
&'a self,
request: &'a FeatureRequest<CompletionParams>,
) -> Vec<CompletionItem> {
) -> Vec<Arc<CompletionItem>> {
LatexCombinators::argument(&request, &COMMAND_NAMES[0..1], 1, async move |_| {
self.items.clone()
})
Expand All @@ -33,7 +35,7 @@ impl LatexColorModelCompletionProvider {
async fn execute_define_color_set<'a>(
&'a self,
request: &'a FeatureRequest<CompletionParams>,
) -> Vec<CompletionItem> {
) -> Vec<Arc<CompletionItem>> {
LatexCombinators::argument(&request, &COMMAND_NAMES[1..2], 0, async move |_| {
self.items.clone()
})
Expand All @@ -43,13 +45,10 @@ impl LatexColorModelCompletionProvider {

impl FeatureProvider for LatexColorModelCompletionProvider {
type Params = CompletionParams;
type Output = Vec<CompletionItem>;
type Output = Vec<Arc<CompletionItem>>;

#[boxed]
async fn execute<'a>(
&'a self,
request: &'a FeatureRequest<CompletionParams>,
) -> Vec<CompletionItem> {
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
let mut items = Vec::new();
items.append(&mut self.execute_define_color(&request).await);
items.append(&mut self.execute_define_color_set(&request).await);
Expand Down
12 changes: 6 additions & 6 deletions src/completion/latex/combinators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ impl LatexCombinators {
pub async fn command<E, F>(
request: &FeatureRequest<CompletionParams>,
execute: E,
) -> Vec<CompletionItem>
) -> Vec<Arc<CompletionItem>>
where
E: FnOnce(Arc<LatexCommand>) -> F,
F: std::future::Future<Output = Vec<CompletionItem>>,
F: std::future::Future<Output = Vec<Arc<CompletionItem>>>,
{
if let SyntaxTree::Latex(tree) = &request.document.tree {
if let Some(command) = tree.find_command(request.params.position) {
Expand All @@ -28,10 +28,10 @@ impl LatexCombinators {
command_names: &'a [&'a str],
argument_index: usize,
execute: E,
) -> Vec<CompletionItem>
) -> Vec<Arc<CompletionItem>>
where
E: FnOnce(Arc<LatexCommand>) -> F,
F: std::future::Future<Output = Vec<CompletionItem>>,
F: std::future::Future<Output = Vec<Arc<CompletionItem>>>,
{
let find_command = |nodes: &[LatexNode], node_index: usize| {
if let LatexNode::Group(group) = &nodes[node_index] {
Expand Down Expand Up @@ -85,10 +85,10 @@ impl LatexCombinators {
pub async fn environment<E, F>(
request: &FeatureRequest<CompletionParams>,
execute: E,
) -> Vec<CompletionItem>
) -> Vec<Arc<CompletionItem>>
where
E: FnOnce(Arc<LatexCommand>) -> F,
F: std::future::Future<Output = Vec<CompletionItem>>,
F: std::future::Future<Output = Vec<Arc<CompletionItem>>>,
{
Self::argument(&request, &ENVIRONMENT_COMMANDS, 0, execute).await
}
Expand Down
Loading

0 comments on commit fd0edad

Please sign in to comment.