Skip to content

Commit

Permalink
Complete usafe of languageserver-types.
Browse files Browse the repository at this point in the history
Note that a case of DidChangeTextDocumentParams need handling.
Closes rust-lang#52
  • Loading branch information
bruno-medeiros committed Oct 26, 2016
1 parent ae7e850 commit 8c9aa5d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 91 deletions.
19 changes: 15 additions & 4 deletions src/actions_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,22 @@ impl ActionHandler {
}
}

pub fn on_change(&self, change: ChangeParams, out: &Output) {
let fname: PathBuf = Url::parse(&change.textDocument.uri).unwrap().to_file_path().unwrap();
let changes: Vec<Change> = change.contentChanges.iter().map(move |i| {
pub fn on_change(&self, change: DidChangeTextDocumentParams, out: &Output) {
let fname: PathBuf = Url::parse(&change.text_document.uri).unwrap().to_file_path().unwrap();
let changes: Vec<Change> = change.content_changes.iter().map(move |i| {
let range = match i.range {
Some(some) => { some }
None => {
// In this case the range is considered to be the whole document,
// as specified by LSP

// FIXME: to do, endpos must be the end of the document, this is not correct
let end_pos = new_pos(0, 0);
Range{ start : new_pos(0, 0), end : end_pos }
}
};
Change {
span: RangeUtil::to_span(i.range, fname.clone()),
span: RangeUtil::to_span(range, fname.clone()),
text: i.text.clone()
}
}).collect();
Expand Down
4 changes: 2 additions & 2 deletions src/ls_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ enum Method {
#[derive(Debug)]
enum Notification {
CancelRequest(NumberOrString),
Change(ChangeParams),
Change(DidChangeTextDocumentParams),
}

/// Creates an public enum whose variants all contain a single serializable payload
Expand Down Expand Up @@ -127,7 +127,7 @@ fn parse_message(input: &str) -> Result<ServerMessage, ParseError> {
Ok(ServerMessage::Request(Request{id: id, method: Method::Hover(method)}))
}
"textDocument/didChange" => {
let method: ChangeParams =
let method: DidChangeTextDocumentParams =
serde_json::from_value(params.unwrap().to_owned()).unwrap();
Ok(ServerMessage::Notification(Notification::Change(method)))
}
Expand Down
110 changes: 25 additions & 85 deletions src/lsp_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use analysis::raw;
use hyper::Url;
use serde::{Serialize};


pub use ls_types::*;

macro_rules! impl_file_name {
($ty_name: ty) => {
impl $ty_name {
Expand All @@ -41,9 +44,9 @@ pub fn to_usize(pos: u64) -> usize {
TryFrom::try_from(pos).unwrap() // FIXME: for this one we definitely need to add error checking
}

pub use ls_types::Position;

pub use ls_types::Range;
pub fn new_pos(line: u64, character: u64,) -> Position {
Position { line: line, character: character }
}

pub struct RangeUtil;

Expand Down Expand Up @@ -72,8 +75,6 @@ impl RangeUtil {
}
}

pub use ls_types::Location;

pub struct LocationUtil;

impl LocationUtil {
Expand Down Expand Up @@ -101,29 +102,6 @@ impl LocationUtil {
}
}

pub use ls_types::InitializeParams;
pub use ls_types::NumberOrString;

pub use ls_types::TextDocumentIdentifier;
pub use ls_types::VersionedTextDocumentIdentifier;

/// An event describing a change to a text document. If range and rangeLength are omitted
/// the new text is considered to be the full content of the document.
#[allow(non_snake_case)]
#[derive(Debug, Deserialize)]
pub struct TextDocumentContentChangeEvent {
pub range: Range,
pub rangeLength: Option<u32>,
pub text: String
}
//pub use ls_types::TextDocumentContentChangeEvent;


pub use ls_types::ReferenceContext;

pub use ls_types::SymbolInformation;
pub use ls_types::SymbolKind;

pub fn sk_from_def_kind(k: raw::DefKind) -> SymbolKind {
match k {
raw::DefKind::Enum => SymbolKind::Enum,
Expand All @@ -143,7 +121,25 @@ pub fn sk_from_def_kind(k: raw::DefKind) -> SymbolKind {
}
}

pub fn new_completion_item(label: String, detail: String) -> CompletionItem {
CompletionItem {
label : label,
kind: None,
detail: Some(detail),
documentation: None,
sort_text: None,
filter_text: None,
insert_text: None,
text_edit: None,
additional_text_edits: None,
command: None,
data: None,
}
}

pub type HoverParams = TextDocumentPositionParams;

/* ----------------- These are not LSP types: ----------------- */

#[derive(Debug, Deserialize)]
pub struct CompilerMessageCode {
Expand All @@ -158,9 +154,7 @@ pub struct CompilerMessage {
pub spans: Vec<Span>,
}

pub use ls_types::Diagnostic;
pub use ls_types::DiagnosticSeverity;
pub use ls_types::PublishDiagnosticsParams;
/* ----------------- These are not LSP types either, but JSON-RPC stuff : ----------------- */

/// An event-like (no response needed) notification message.
#[derive(Debug, Serialize)]
Expand All @@ -181,57 +175,3 @@ impl <T> NotificationMessage<T> where T: Debug + Serialize {
}
}
}

pub use ls_types::WorkspaceEdit;

pub use ls_types::ReferenceParams;
pub use ls_types::TextDocumentPositionParams;

#[allow(non_snake_case)]
#[derive(Debug, Deserialize)]
pub struct ChangeParams {
pub textDocument: VersionedTextDocumentIdentifier,
pub contentChanges: Vec<TextDocumentContentChangeEvent>
}

pub type HoverParams = TextDocumentPositionParams;
pub use ls_types::RenameParams;
pub use ls_types::DocumentSymbolParams;


pub use ls_types::CancelParams;
pub use ls_types::TextDocumentSyncKind;

pub use ls_types::MarkedString;

pub use ls_types::Hover;
pub use ls_types::InitializeResult;

pub use ls_types::CompletionItem;

pub fn new_completion_item(label: String, detail: String) -> CompletionItem {
CompletionItem {
label : label,
kind: None,
detail: Some(detail),
documentation: None,
sort_text: None,
filter_text: None,
insert_text: None,
text_edit: None,
additional_text_edits: None,
command: None,
data: None,
}
}

pub use ls_types::TextEdit;
pub use ls_types::CompletionOptions;

pub use ls_types::SignatureHelpOptions;

pub use ls_types::DocumentFormattingParams;
pub use ls_types::DocumentRangeFormattingParams;
pub use ls_types::FormattingOptions;

pub use ls_types::ServerCapabilities;

0 comments on commit 8c9aa5d

Please sign in to comment.