Skip to content

Commit

Permalink
Refactor protocol crate
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Dec 31, 2019
1 parent 7faf67a commit f22be76
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 22 deletions.
102 changes: 92 additions & 10 deletions crates/texlab_protocol/src/capabilities.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use lsp_types::{ClientCapabilities, MarkupKind};
use lsp_types::*;

pub trait ClientCapabilitiesExt {
fn has_definition_link_support(&self) -> bool;

fn has_hierarchical_document_symbol_support(&self) -> bool;

fn has_work_done_progress(&self) -> bool;
fn has_work_done_progress_support(&self) -> bool;

fn has_hover_markdown_support(&self) -> bool;
}
Expand All @@ -27,20 +27,102 @@ impl ClientCapabilitiesExt for ClientCapabilities {
== Some(true)
}

fn has_work_done_progress(&self) -> bool {
fn has_work_done_progress_support(&self) -> bool {
self.window.as_ref().and_then(|cap| cap.work_done_progress) == Some(true)
}

fn has_hover_markdown_support(&self) -> bool {
if let Some(formats) = self
.text_document
self.text_document
.as_ref()
.and_then(|cap| cap.hover.as_ref())
.and_then(|cap| cap.content_format.as_ref())
{
formats.contains(&MarkupKind::Markdown)
} else {
false
}
.filter(|formats| formats.contains(&MarkupKind::Markdown))
.is_some()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn has_definition_link_support_true() {
let capabilities = ClientCapabilities {
text_document: Some(TextDocumentClientCapabilities {
definition: Some(GotoCapability {
link_support: Some(true),
..GotoCapability::default()
}),
..TextDocumentClientCapabilities::default()
}),
..ClientCapabilities::default()
};
assert!(capabilities.has_definition_link_support());
}

#[test]
fn has_definition_link_support_false() {
let capabilities = ClientCapabilities::default();
assert!(!capabilities.has_definition_link_support());
}

#[test]
fn has_hierarchical_document_symbol_support_true() {
let capabilities = ClientCapabilities {
text_document: Some(TextDocumentClientCapabilities {
document_symbol: Some(DocumentSymbolCapability {
hierarchical_document_symbol_support: Some(true),
..DocumentSymbolCapability::default()
}),
..TextDocumentClientCapabilities::default()
}),
..ClientCapabilities::default()
};
assert!(capabilities.has_hierarchical_document_symbol_support());
}

#[test]
fn has_hierarchical_document_symbol_support_false() {
let capabilities = ClientCapabilities::default();
assert!(!capabilities.has_hierarchical_document_symbol_support());
}

#[test]
fn has_work_done_progress_support_true() {
let capabilities = ClientCapabilities {
window: Some(WindowClientCapabilities {
work_done_progress: Some(true),
..WindowClientCapabilities::default()
}),
..ClientCapabilities::default()
};
assert!(capabilities.has_work_done_progress_support());
}

#[test]
fn has_work_done_progress_support_false() {
let capabilities = ClientCapabilities::default();
assert!(!capabilities.has_work_done_progress_support());
}

#[test]
fn has_hover_markdown_support_true() {
let capabilities = ClientCapabilities {
text_document: Some(TextDocumentClientCapabilities {
hover: Some(HoverCapability {
content_format: Some(vec![MarkupKind::PlainText, MarkupKind::Markdown]),
..HoverCapability::default()
}),
..TextDocumentClientCapabilities::default()
}),
..ClientCapabilities::default()
};
assert!(capabilities.has_hover_markdown_support());
}

#[test]
fn has_hover_markdown_support_false() {
let capabilities = ClientCapabilities::default();
assert!(!capabilities.has_hover_markdown_support());
}
}
18 changes: 9 additions & 9 deletions crates/texlab_protocol/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,60 +85,60 @@ mod parser {
use super::*;

#[test]
fn test_parse_content_type() {
fn parse_content_type() {
let result =
content_type(b"Content-Type: application/vscode-jsonrpc;charset=utf-8\r\n");
assert!(result.is_ok());
}

#[test]
fn test_parse_content_type_utf8() {
fn parse_content_type_utf8() {
let result = content_type(b"Content-Type: application/vscode-jsonrpc;charset=utf8\r\n");
assert!(result.is_ok());
}

#[test]
fn test_parse_content_length() {
fn parse_content_length() {
let result = content_length(b"Content-Length: 42\r\n");
assert_eq!(result.unwrap().1, 42usize);
}

#[test]
fn test_parse_message_full() {
fn parse_message_full() {
let result = parse(
b"Content-Length: 2\r\nContent-Type: application/vscode-jsonrpc;charset=utf8\r\n\r\n{}",
);
assert_eq!(result.unwrap().1, "{}");
}

#[test]
fn test_parse_message_type_first() {
fn parse_message_type_first() {
let result = parse(
b"Content-Type: application/vscode-jsonrpc;charset=utf8\r\nContent-Length: 2\r\n\r\n{}",
);
assert_eq!(result.unwrap().1, "{}");
}

#[test]
fn test_parse_message_without_type() {
fn parse_message_without_type() {
let result = parse(b"Content-Length: 2\r\n\r\n{}");
assert_eq!(result.unwrap().1, "{}");
}

#[test]
fn test_parse_message_incomplete() {
fn parse_message_incomplete() {
let result = parse(b"Content-Length:");
assert!(result.unwrap_err().is_incomplete());
}

#[test]
fn test_parse_message_invalid() {
fn parse_message_invalid() {
let error = parse(b"foo").unwrap_err();
assert!(!error.is_incomplete());
}

#[test]
fn test_parse_message_overflow() {
fn parse_message_overflow() {
let result = parse(b"Content-Length: 4\r\n\r\n{}");
assert!(result.unwrap_err().is_incomplete());
}
Expand Down
67 changes: 66 additions & 1 deletion crates/texlab_protocol/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl RangeExt for Range {
end_line: u64,
end_character: u64,
) -> Self {
Range {
Self {
start: Position::new(start_line, start_character),
end: Position::new(end_line, end_character),
}
Expand All @@ -30,3 +30,68 @@ impl RangeExt for Range {
position > self.start && position < self.end
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn contains_inside() {
let range = Range::new_simple(1, 2, 3, 4);
assert!(range.contains(Position::new(2, 5)));
}

#[test]
fn contains_begin() {
let range = Range::new_simple(1, 2, 3, 4);
assert!(range.contains(range.start));
}

#[test]
fn contains_end() {
let range = Range::new_simple(1, 2, 3, 4);
assert!(range.contains(range.end));
}

#[test]
fn contains_outside_left() {
let range = Range::new_simple(1, 2, 3, 4);
assert!(!range.contains(Position::new(0, 5)));
}

#[test]
fn contains_outside_right() {
let range = Range::new_simple(1, 2, 3, 4);
assert!(!range.contains(Position::new(5, 1)));
}

#[test]
fn contains_exclusive_inside() {
let range = Range::new_simple(1, 2, 3, 4);
assert!(range.contains_exclusive(Position::new(2, 5)));
}

#[test]
fn contains_exclusive_begin() {
let range = Range::new_simple(1, 2, 3, 4);
assert!(!range.contains_exclusive(range.start));
}

#[test]
fn contains_exclusive_end() {
let range = Range::new_simple(1, 2, 3, 4);
assert!(!range.contains_exclusive(range.end));
}

#[test]
fn contains_exclusive_outside_left() {
let range = Range::new_simple(1, 2, 3, 4);
assert!(!range.contains_exclusive(Position::new(0, 5)));
}

#[test]
fn contains_exclusive_outside_right() {
let range = Range::new_simple(1, 2, 3, 4);
assert!(!range.contains_exclusive(Position::new(5, 1)));
}
}
4 changes: 2 additions & 2 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ where

match document.uri.to_file_path() {
Ok(path) => {
if request.client_capabilities.has_work_done_progress() {
if request.client_capabilities.has_work_done_progress_support() {
let params = WorkDoneProgressCreateParams {
token: self.token.clone(),
};
Expand Down Expand Up @@ -151,7 +151,7 @@ where
},
};

if request.client_capabilities.has_work_done_progress() {
if request.client_capabilities.has_work_done_progress_support() {
let params = ProgressParams {
token: provider.token.clone(),
value: ProgressParamsValue::WorkDone(WorkDoneProgress::Done(
Expand Down

0 comments on commit f22be76

Please sign in to comment.