Skip to content

Commit

Permalink
Fix resolution of imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed May 18, 2019
1 parent ed2a3bf commit 0913425
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/link/latex_include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl LatexIncludeLinkProvider {
) -> Option<DocumentLink> {
request
.workspace
.resolve_document(&request.document.uri, include.path().text())
.resolve_document(&request.document.uri, include)
.map(|target| DocumentLink {
range: include.path().range(),
target: target.uri.clone(),
Expand Down
70 changes: 36 additions & 34 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ impl Document {
}
}

const DOCUMENT_EXTENSIONS: &'static [&'static str] = &[".tex", ".sty", ".cls", ".bib"];

#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct Workspace {
pub documents: Vec<Arc<Document>>,
Expand All @@ -51,28 +49,52 @@ impl Workspace {
.map(|document| Arc::clone(&document))
}

pub fn resolve_document(&self, uri: &Uri, relative_path: &str) -> Option<Arc<Document>> {
let targets = resolve_link_targets(uri, relative_path)?;
for target in targets {
if let Ok(target_uri) = Uri::from_file_path(target) {
if let Some(document) = self.find(&target_uri) {
if document.is_file() {
return Some(document);
}
}
pub fn find_path(&self, path: &str) -> Option<Arc<Document>> {
self.documents
.iter()
.find(|document| document.is_file() && document.uri.path() == path)
.map(|document| Arc::clone(&document))
}

pub fn resolve_document(&self, uri: &Uri, include: &LatexInclude) -> Option<Arc<Document>> {
let targets = Self::resolve_link_targets(uri, include)?;
for target in &targets {
if let Some(document) = self.find_path(target) {
return Some(document);
}
}
None
}

fn resolve_link_targets(uri: &Uri, include: &LatexInclude) -> Option<[String; 2]> {
if uri.scheme() != "file" {
return None;
}

let mut path = uri.to_file_path().ok()?;
path.pop();
path.push(include.path().text());
path = PathBuf::from(path.to_string_lossy().replace("\\", "/"));
path = path.clean();
let path1 = path.to_string_lossy().into_owned();

let extension = match include.kind {
LatexIncludeKind::Package => ".sty",
LatexIncludeKind::Class => ".cls",
LatexIncludeKind::TexFile => ".tex",
LatexIncludeKind::BibFile => ".bib",
};
let path2 = format!("{}{}", path1, extension);

Some([path1, path2])
}

pub fn related_documents(&self, uri: &Uri) -> Vec<Arc<Document>> {
let mut edges: Vec<(Arc<Document>, Arc<Document>)> = Vec::new();
for parent in self.documents.iter().filter(|document| document.is_file()) {
if let SyntaxTree::Latex(tree) = &parent.tree {
for include in &tree.includes {
if let Some(ref child) =
self.resolve_document(&parent.uri, include.path().text())
{
if let Some(ref child) = self.resolve_document(&parent.uri, include) {
edges.push((Arc::clone(&parent), Arc::clone(&child)));
edges.push((Arc::clone(&child), Arc::clone(&parent)));
}
Expand Down Expand Up @@ -114,26 +136,6 @@ impl Workspace {
}
}

fn resolve_link_targets(uri: &Uri, relative_path: &str) -> Option<Vec<String>> {
let mut targets = Vec::new();
if uri.scheme() != "file" {
return None;
}

let mut path = uri.to_file_path().ok()?;
path.pop();
path.push(relative_path);
path = PathBuf::from(path.to_string_lossy().replace("\\", "/"));
path = path.clean();
let path = path.to_string_lossy().into_owned();
for extension in DOCUMENT_EXTENSIONS {
targets.push(format!("{}{}", path, extension));
}
targets.push(path);
targets.reverse();
Some(targets)
}

pub struct WorkspaceManager {
workspace: Mutex<Arc<Workspace>>,
}
Expand Down

0 comments on commit 0913425

Please sign in to comment.