Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up language server and make sure all data structures are localized to files to make them easier to reload in isolation #47

Merged
merged 8 commits into from
Apr 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]

- language-server: Support to find references ([#34]).
- language-server: clean up and fix bugs ([#47]).

[Unreleased]: https://github.com/reproto/reproto/compare/0.3.37...master

Expand Down
86 changes: 39 additions & 47 deletions lib/languageserver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ extern crate url;
extern crate url_serde;

mod envelope;
mod loaded_file;
mod models;
mod workspace;

use self::ContentType::*;
use self::workspace::{Completion, Jump, LoadedFile, Range, RenameResult, Workspace};
use self::loaded_file::LoadedFile;
use self::models::{Completion, Jump, Range, RenameResult};
use self::workspace::Workspace;
use core::errors::Result;
use core::{Context, ContextItem, Diagnostics, Encoding, RealFilesystem, Source};
use ropey::Rope;
Expand Down Expand Up @@ -831,7 +835,7 @@ where
let mut path = key.clone();
path.push(s.name.to_string());

let range = convert_range(s.range.start, s.range.end);
let range = convert_range(s.range);

let location = ty::Location {
uri: s.url.clone(),
Expand Down Expand Up @@ -892,11 +896,9 @@ where
if let Some(references) = workspace.find_reference(&url, params.position) {
for (url, ranges) in references {
for r in ranges {
let range = convert_range(r.start, r.end);

locations.push(ty::Location {
uri: url.clone(),
range: range,
range: convert_range(r),
});
}
}
Expand Down Expand Up @@ -958,7 +960,7 @@ where
match *d {
core::Diagnostic::Error(ref span, ref m) => {
let (start, end) = file.diag.source.span_to_range(*span, Encoding::Utf16)?;
let range = convert_range(start, end);
let range = convert_range((start, end));

let d = ty::Diagnostic {
range: range,
Expand All @@ -971,7 +973,7 @@ where
}
core::Diagnostic::Info(ref span, ref m) => {
let (start, end) = file.diag.source.span_to_range(*span, Encoding::Utf16)?;
let range = convert_range(start, end);
let range = convert_range((start, end));

let d = ty::Diagnostic {
range: range,
Expand Down Expand Up @@ -1366,34 +1368,32 @@ where
}
// A collection of ranges from different URLs that should be changed.
RenameResult::Collections { ranges } => {
if let Some(ranges) = ranges {
let mut changes = Vec::new();

for (url, ranges) in ranges {
let edits = setup_edits(ranges, new_name.as_str());

changes.push(ty::TextDocumentEdit {
text_document: ty::VersionedTextDocumentIdentifier {
uri: url.clone(),
version: None,
},
edits: edits,
});
}
let mut changes = Vec::new();

for (url, ranges) in ranges {
let edits = setup_edits(ranges, new_name.as_str());

edit = Some(ty::WorkspaceEdit {
document_changes: Some(changes),
..ty::WorkspaceEdit::default()
changes.push(ty::TextDocumentEdit {
text_document: ty::VersionedTextDocumentIdentifier {
uri: url.clone(),
version: None,
},
edits: edits,
});
}

edit = Some(ty::WorkspaceEdit {
document_changes: Some(changes),
..ty::WorkspaceEdit::default()
});
}
// Special case: identical to Local, but we also want to refactor the package
// position to include the new alias.
RenameResult::ImplicitPackage { ranges, position } => {
let mut edits = setup_edits(ranges, new_name.as_str());

edits.push(ty::TextEdit {
range: convert_range(position, position),
range: convert_range((position, position)),
new_text: format!(" as {}", new_name),
});

Expand All @@ -1415,7 +1415,7 @@ where

for range in ranges {
edits.push(ty::TextEdit {
range: convert_range(range.start, range.end),
range: convert_range(range),
new_text: new_text.to_string(),
});
}
Expand Down Expand Up @@ -1468,16 +1468,11 @@ where

match *value {
Jump::Absolute {
ref prefix,
ref package,
ref path,
} => {
let (uri, file) = if let Some(ref prefix) = *prefix {
let prefix = match file.prefixes.get(prefix) {
Some(prefix) => prefix,
None => return Ok(()),
};

let url = match workspace.packages.get(&prefix.package) {
let (uri, file) = if let Some(ref package) = *package {
let url = match workspace.packages.get(package) {
Some(url) => url,
None => return Ok(()),
};
Expand All @@ -1496,18 +1491,13 @@ where
};

let (start, end) = file.diag.source.span_to_range(span, Encoding::Utf16)?;
let range = convert_range(start, end);
let range = convert_range((start, end));
let location = ty::Location { uri, range };

*response = Some(ty::request::GotoDefinitionResponse::Scalar(location));
}
Jump::Package { ref prefix } => {
let prefix = match file.prefixes.get(prefix) {
Some(prefix) => prefix,
None => return Ok(()),
};

let uri = match workspace.packages.get(&prefix.package) {
Jump::Package { ref package } => {
let uri = match workspace.packages.get(package) {
Some(url) => url.clone(),
None => return Ok(()),
};
Expand All @@ -1522,10 +1512,7 @@ where
None => return Ok(()),
};

let (start, end) = file.diag
.source
.span_to_range(prefix.span, Encoding::Utf16)?;
let range = convert_range(start, end);
let range = convert_range(prefix.range);

let location = ty::Location {
uri: url.clone(),
Expand All @@ -1541,7 +1528,12 @@ where
}

/// Convert an internal range into a language-server range.
fn convert_range(start: core::Position, end: core::Position) -> ty::Range {
fn convert_range<R: Into<Range>>(range: R) -> ty::Range {
let range = range.into();

let start = range.start;
let end = range.end;

let start = ty::Position {
line: start.line as u64,
character: start.col as u64,
Expand Down
Loading