summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/completion/include.rs
diff options
context:
space:
mode:
Diffstat (limited to 'support/texlab/src/features/completion/include.rs')
-rw-r--r--support/texlab/src/features/completion/include.rs78
1 files changed, 37 insertions, 41 deletions
diff --git a/support/texlab/src/features/completion/include.rs b/support/texlab/src/features/completion/include.rs
index d851aca820..bde7544570 100644
--- a/support/texlab/src/features/completion/include.rs
+++ b/support/texlab/src/features/completion/include.rs
@@ -4,18 +4,22 @@ use std::{
path::{Path, PathBuf},
};
-use lsp_types::CompletionParams;
use rowan::{ast::AstNode, TextRange, TextSize};
-use crate::{features::cursor::CursorContext, syntax::latex};
+use crate::{syntax::latex, util::cursor::CursorContext};
-use super::types::{InternalCompletionItem, InternalCompletionItemData};
+use super::builder::CompletionBuilder;
-pub fn complete_includes<'a>(
- context: &'a CursorContext<CompletionParams>,
- items: &mut Vec<InternalCompletionItem<'a>>,
+pub fn complete<'db>(
+ context: &'db CursorContext,
+ builder: &mut CompletionBuilder<'db>,
) -> Option<()> {
- if context.request.main_document().uri.scheme() != "file" {
+ if context
+ .document
+ .location(context.db)
+ .path(context.db)
+ .is_none()
+ {
return None;
}
@@ -51,10 +55,15 @@ pub fn complete_includes<'a>(
let mut dirs = vec![current_dir(context, &path_text, None)];
if include.kind() == latex::GRAPHICS_INCLUDE {
- for document in context.request.workspace.documents_by_uri.values() {
- if let Some(data) = document.data.as_latex() {
- for graphics_path in &data.extras.graphics_paths {
- dirs.push(current_dir(context, &path_text, Some(graphics_path)));
+ for document in context.related() {
+ if let Some(data) = document.parse(context.db).as_tex() {
+ for path in data
+ .analyze(context.db)
+ .graphics_paths(context.db)
+ .iter()
+ .map(|node| node.path(context.db))
+ {
+ dirs.push(current_dir(context, &path_text, Some(path)));
}
}
}
@@ -74,15 +83,12 @@ pub fn complete_includes<'a>(
if !include_extension {
remove_extension(&mut path);
}
+
let name = path.file_name()?.to_str()?.into();
- let data = InternalCompletionItemData::File { name };
- let item = InternalCompletionItem::new(segment_range, data);
- items.push(item);
+ builder.file(segment_range, name);
} else if file_type.is_dir() {
let name = path.file_name()?.to_str()?.into();
- let data = InternalCompletionItemData::Directory { name };
- let item = InternalCompletionItem::new(segment_range, data);
- items.push(item);
+ builder.directory(segment_range, name);
}
}
@@ -90,34 +96,24 @@ pub fn complete_includes<'a>(
}
fn current_dir(
- context: &CursorContext<CompletionParams>,
+ context: &CursorContext,
path_text: &str,
graphics_path: Option<&str>,
) -> Option<PathBuf> {
- let mut path = context
- .request
+ let parent = context
.workspace
- .environment
- .options
- .root_directory
- .as_ref()
- .map_or_else(
- || {
- let mut path = context.request.main_document().uri.to_file_path().unwrap();
- path.pop();
- path
- },
- |root_directory| {
- context
- .request
- .workspace
- .environment
- .current_directory
- .join(root_directory)
- },
- );
-
- path = PathBuf::from(path.to_str()?.replace('\\', "/"));
+ .parents(context.db, context.document)
+ .iter()
+ .next()
+ .map_or(context.document, Clone::clone);
+
+ let path = context
+ .workspace
+ .working_dir(context.db, parent.directory(context.db))
+ .path(context.db)
+ .as_deref()?;
+
+ let mut path = PathBuf::from(path.to_str()?.replace('\\', "/"));
if !path_text.is_empty() {
if let Some(graphics_path) = graphics_path {
path.push(graphics_path);