From e293a16d6b430befe6eb8be315ad60e7b2b10926 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Sun, 23 Sep 2018 11:10:57 -0400 Subject: Support LSP 3.13 --- crates/ra_lsp_server/src/caps.rs | 6 +++++- crates/ra_lsp_server/src/main_loop/handlers.rs | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/caps.rs b/crates/ra_lsp_server/src/caps.rs index 7456aea8a..3c628f29c 100644 --- a/crates/ra_lsp_server/src/caps.rs +++ b/crates/ra_lsp_server/src/caps.rs @@ -1,5 +1,7 @@ use languageserver_types::{ ServerCapabilities, + CodeActionProviderCapability, + FoldingRangeProviderCapability, TextDocumentSyncCapability, TextDocumentSyncOptions, TextDocumentSyncKind, @@ -32,7 +34,7 @@ pub fn server_capabilities() -> ServerCapabilities { document_highlight_provider: None, document_symbol_provider: Some(true), workspace_symbol_provider: Some(true), - code_action_provider: Some(true), + code_action_provider: Some(CodeActionProviderCapability::Simple(true)), code_lens_provider: None, document_formatting_provider: None, document_range_formatting_provider: None, @@ -40,10 +42,12 @@ pub fn server_capabilities() -> ServerCapabilities { first_trigger_character: "=".to_string(), more_trigger_character: None, }), + folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)), rename_provider: None, color_provider: None, execute_command_provider: Some(ExecuteCommandOptions { commands: vec!["apply_code_action".to_string()], }), + workspace: None, } } diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index b2ebc9cdc..2d9391859 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use languageserver_types::{ Diagnostic, DiagnosticSeverity, DocumentSymbol, - Command, TextDocumentIdentifier, + CodeActionResponse, Command, TextDocumentIdentifier, SymbolInformation, Position, Location, TextEdit, CompletionItem, InsertTextFormat, CompletionItemKind, }; @@ -369,7 +369,7 @@ pub fn handle_code_action( world: ServerWorld, params: req::CodeActionParams, _token: JobToken, -) -> Result>> { +) -> Result> { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); let range = params.range.conv_with(&line_index); @@ -392,7 +392,7 @@ pub fn handle_code_action( res.push(cmd); } - Ok(Some(res)) + Ok(Some(CodeActionResponse::Commands(res))) } pub fn publish_diagnostics( -- cgit v1.2.3 From bd2b2f1b48f86d59c3b746b72a14192f75419a84 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Sun, 23 Sep 2018 11:13:27 -0400 Subject: Implement folding ranges --- crates/ra_lsp_server/src/main_loop/handlers.rs | 90 +++++++++++++++++++++++++- crates/ra_lsp_server/src/main_loop/mod.rs | 1 + 2 files changed, 90 insertions(+), 1 deletion(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 2d9391859..36cdeda38 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -1,15 +1,18 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use languageserver_types::{ Diagnostic, DiagnosticSeverity, DocumentSymbol, CodeActionResponse, Command, TextDocumentIdentifier, SymbolInformation, Position, Location, TextEdit, CompletionItem, InsertTextFormat, CompletionItemKind, + FoldingRange, FoldingRangeParams, FoldingRangeKind }; use serde_json::to_value; use ra_analysis::{Query, FileId, RunnableKind, JobToken}; use ra_syntax::{ + algo::{siblings, walk, Direction}, text_utils::contains_offset_nonstrict, + SyntaxKind, SyntaxNodeRef, TextRange }; use ::{ @@ -177,6 +180,7 @@ pub fn handle_workspace_symbol( world, &line_index )?, container_name: None, + deprecated: None, }; res.push(info); }; @@ -365,6 +369,90 @@ pub fn handle_completion( Ok(Some(req::CompletionResponse::Array(items))) } +pub fn handle_folding_range( + world: ServerWorld, + params: FoldingRangeParams, + _token: JobToken, +) -> Result>> { + let file_id = params.text_document.try_conv_with(&world)?; + let file = world.analysis().file_syntax(file_id); + let line_index = world.analysis().file_line_index(file_id); + let syntax = file.syntax(); + + let mut res = vec![]; + let mut visited = HashSet::new(); + + for node in walk::preorder(syntax) { + if visited.contains(&node) { + continue; + } + + let range_and_kind = match node.kind() { + SyntaxKind::COMMENT => ( + contiguous_range_for(SyntaxKind::COMMENT, node, &mut visited), + Some(FoldingRangeKind::Comment), + ), + SyntaxKind::USE_ITEM => ( + contiguous_range_for(SyntaxKind::USE_ITEM, node, &mut visited), + Some(FoldingRangeKind::Imports), + ), + _ => (None, None), + }; + + match range_and_kind { + (Some(range), Some(kind)) => { + let range = range.conv_with(&line_index); + res.push(FoldingRange { + start_line: range.start.line, + start_character: Some(range.start.character), + end_line: range.end.line, + end_character: Some(range.start.character), + kind: Some(kind), + }); + } + _ => {} + } + } + + if res.is_empty() { + Ok(None) + } else { + Ok(Some(res)) + } +} + +fn contiguous_range_for<'a>( + kind: SyntaxKind, + node: SyntaxNodeRef<'a>, + visited: &mut HashSet>, +) -> Option { + visited.insert(node); + + let left = node; + let mut right = node; + for node in siblings(node, Direction::Forward) { + visited.insert(node); + match node.kind() { + SyntaxKind::WHITESPACE if !node.leaf_text().unwrap().as_str().contains("\n\n") => (), + k => { + if k == kind { + right = node + } else { + break; + } + } + } + } + if left != right { + Some(TextRange::from_to( + left.range().start(), + right.range().end(), + )) + } else { + None + } +} + pub fn handle_code_action( world: ServerWorld, params: req::CodeActionParams, diff --git a/crates/ra_lsp_server/src/main_loop/mod.rs b/crates/ra_lsp_server/src/main_loop/mod.rs index 2b2279e97..abc58b70e 100644 --- a/crates/ra_lsp_server/src/main_loop/mod.rs +++ b/crates/ra_lsp_server/src/main_loop/mod.rs @@ -253,6 +253,7 @@ fn on_request( .on::(handlers::handle_decorations)? .on::(handlers::handle_completion)? .on::(handlers::handle_code_action)? + .on::(handlers::handle_folding_range)? .finish(); match req { Ok((id, handle)) => { -- cgit v1.2.3 From ff0a706a30567f297642ba1fa6ee9537ed82c40f Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Mon, 24 Sep 2018 09:52:33 -0400 Subject: Split folding ranges into editor and lsp parts --- crates/ra_lsp_server/src/main_loop/handlers.rs | 98 ++++++-------------------- 1 file changed, 21 insertions(+), 77 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 36cdeda38..51061543c 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::{HashMap}; use languageserver_types::{ Diagnostic, DiagnosticSeverity, DocumentSymbol, @@ -8,11 +8,9 @@ use languageserver_types::{ FoldingRange, FoldingRangeParams, FoldingRangeKind }; use serde_json::to_value; -use ra_analysis::{Query, FileId, RunnableKind, JobToken}; +use ra_analysis::{Query, FileId, RunnableKind, JobToken, FoldKind}; use ra_syntax::{ - algo::{siblings, walk, Direction}, - text_utils::contains_offset_nonstrict, - SyntaxKind, SyntaxNodeRef, TextRange + text_utils::contains_offset_nonstrict }; use ::{ @@ -375,82 +373,28 @@ pub fn handle_folding_range( _token: JobToken, ) -> Result>> { let file_id = params.text_document.try_conv_with(&world)?; - let file = world.analysis().file_syntax(file_id); let line_index = world.analysis().file_line_index(file_id); - let syntax = file.syntax(); - - let mut res = vec![]; - let mut visited = HashSet::new(); - - for node in walk::preorder(syntax) { - if visited.contains(&node) { - continue; - } - - let range_and_kind = match node.kind() { - SyntaxKind::COMMENT => ( - contiguous_range_for(SyntaxKind::COMMENT, node, &mut visited), - Some(FoldingRangeKind::Comment), - ), - SyntaxKind::USE_ITEM => ( - contiguous_range_for(SyntaxKind::USE_ITEM, node, &mut visited), - Some(FoldingRangeKind::Imports), - ), - _ => (None, None), - }; - match range_and_kind { - (Some(range), Some(kind)) => { - let range = range.conv_with(&line_index); - res.push(FoldingRange { - start_line: range.start.line, - start_character: Some(range.start.character), - end_line: range.end.line, - end_character: Some(range.start.character), - kind: Some(kind), - }); + let res = Some(world.analysis() + .folding_ranges(file_id) + .into_iter() + .map(|fold| { + let kind = match fold.kind { + FoldKind::Comment => FoldingRangeKind::Comment, + FoldKind::Imports => FoldingRangeKind::Imports + }; + let range = fold.range.conv_with(&line_index); + FoldingRange { + start_line: range.start.line, + start_character: Some(range.start.character), + end_line: range.end.line, + end_character: Some(range.start.character), + kind: Some(kind) } - _ => {} - } - } - - if res.is_empty() { - Ok(None) - } else { - Ok(Some(res)) - } -} + }) + .collect()); -fn contiguous_range_for<'a>( - kind: SyntaxKind, - node: SyntaxNodeRef<'a>, - visited: &mut HashSet>, -) -> Option { - visited.insert(node); - - let left = node; - let mut right = node; - for node in siblings(node, Direction::Forward) { - visited.insert(node); - match node.kind() { - SyntaxKind::WHITESPACE if !node.leaf_text().unwrap().as_str().contains("\n\n") => (), - k => { - if k == kind { - right = node - } else { - break; - } - } - } - } - if left != right { - Some(TextRange::from_to( - left.range().start(), - right.range().end(), - )) - } else { - None - } + Ok(res) } pub fn handle_code_action( -- cgit v1.2.3