diff options
author | Jeremy A. Kolb <[email protected]> | 2018-10-30 18:07:14 +0000 |
---|---|---|
committer | Jeremy A. Kolb <[email protected]> | 2018-10-31 19:23:20 +0000 |
commit | 05eb489ec58fbcd36f8d68c74e42166b345a3a3f (patch) | |
tree | 17083b18fd209334833423728df1a170ea7a3905 | |
parent | d2bcd1a38636a669d2232156fa29010a82e37a3e (diff) |
Useful comments from function signatures
-rw-r--r-- | crates/ra_analysis/src/descriptors/function/mod.rs | 60 | ||||
-rw-r--r-- | crates/ra_analysis/tests/tests.rs | 147 | ||||
-rw-r--r-- | crates/ra_editor/src/extend_selection.rs | 2 | ||||
-rw-r--r-- | crates/ra_editor/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_lsp_server/src/main_loop/handlers.rs | 13 |
5 files changed, 217 insertions, 7 deletions
diff --git a/crates/ra_analysis/src/descriptors/function/mod.rs b/crates/ra_analysis/src/descriptors/function/mod.rs index bb68b0ce7..ae40f3e8f 100644 --- a/crates/ra_analysis/src/descriptors/function/mod.rs +++ b/crates/ra_analysis/src/descriptors/function/mod.rs | |||
@@ -1,8 +1,11 @@ | |||
1 | pub(super) mod imp; | 1 | pub(super) mod imp; |
2 | mod scope; | 2 | mod scope; |
3 | 3 | ||
4 | use std::cmp::{min, max}; | ||
5 | |||
4 | use ra_syntax::{ | 6 | use ra_syntax::{ |
5 | ast::{self, AstNode, NameOwner} | 7 | ast::{self, AstNode, DocCommentsOwner, NameOwner}, |
8 | TextRange, TextUnit | ||
6 | }; | 9 | }; |
7 | 10 | ||
8 | use crate::{ | 11 | use crate::{ |
@@ -30,14 +33,17 @@ pub struct FnDescriptor { | |||
30 | pub label: String, | 33 | pub label: String, |
31 | pub ret_type: Option<String>, | 34 | pub ret_type: Option<String>, |
32 | pub params: Vec<String>, | 35 | pub params: Vec<String>, |
36 | pub doc: Option<String> | ||
33 | } | 37 | } |
34 | 38 | ||
35 | impl FnDescriptor { | 39 | impl FnDescriptor { |
36 | pub fn new(node: ast::FnDef) -> Option<Self> { | 40 | pub fn new(node: ast::FnDef) -> Option<Self> { |
37 | let name = node.name()?.text().to_string(); | 41 | let name = node.name()?.text().to_string(); |
38 | 42 | ||
43 | let mut doc = None; | ||
44 | |||
39 | // Strip the body out for the label. | 45 | // Strip the body out for the label. |
40 | let label: String = if let Some(body) = node.body() { | 46 | let mut label: String = if let Some(body) = node.body() { |
41 | let body_range = body.syntax().range(); | 47 | let body_range = body.syntax().range(); |
42 | let label: String = node | 48 | let label: String = node |
43 | .syntax() | 49 | .syntax() |
@@ -50,6 +56,36 @@ impl FnDescriptor { | |||
50 | node.syntax().text().to_string() | 56 | node.syntax().text().to_string() |
51 | }; | 57 | }; |
52 | 58 | ||
59 | if let Some((comment_range, docs)) = FnDescriptor::extract_doc_comments(node) { | ||
60 | let comment_range = comment_range.checked_sub(node.syntax().range().start()).unwrap(); | ||
61 | let start = comment_range.start().to_usize(); | ||
62 | let end = comment_range.end().to_usize(); | ||
63 | |||
64 | // Remove the comment from the label | ||
65 | label.replace_range(start..end, ""); | ||
66 | |||
67 | // Massage markdown | ||
68 | let mut processed_lines = Vec::new(); | ||
69 | let mut in_code_block = false; | ||
70 | for line in docs.lines() { | ||
71 | if line.starts_with("```") { | ||
72 | in_code_block = !in_code_block; | ||
73 | } | ||
74 | |||
75 | let line = if in_code_block && line.starts_with("```") && !line.contains("rust") { | ||
76 | "```rust".into() | ||
77 | } else { | ||
78 | line.to_string() | ||
79 | }; | ||
80 | |||
81 | processed_lines.push(line); | ||
82 | } | ||
83 | |||
84 | if !processed_lines.is_empty() { | ||
85 | doc = Some(processed_lines.join("\n")); | ||
86 | } | ||
87 | } | ||
88 | |||
53 | let params = FnDescriptor::param_list(node); | 89 | let params = FnDescriptor::param_list(node); |
54 | let ret_type = node.ret_type().map(|r| r.syntax().text().to_string()); | 90 | let ret_type = node.ret_type().map(|r| r.syntax().text().to_string()); |
55 | 91 | ||
@@ -57,10 +93,28 @@ impl FnDescriptor { | |||
57 | name, | 93 | name, |
58 | ret_type, | 94 | ret_type, |
59 | params, | 95 | params, |
60 | label, | 96 | label: label.trim().to_owned(), |
97 | doc | ||
61 | }) | 98 | }) |
62 | } | 99 | } |
63 | 100 | ||
101 | fn extract_doc_comments(node: ast::FnDef) -> Option<(TextRange, String)> { | ||
102 | if node.doc_comments().count() == 0 { | ||
103 | return None; | ||
104 | } | ||
105 | |||
106 | let comment_text = node.doc_comment_text(); | ||
107 | |||
108 | let (begin, end) = node.doc_comments() | ||
109 | .map(|comment| comment.syntax().range()) | ||
110 | .map(|range| (range.start().to_usize(), range.end().to_usize())) | ||
111 | .fold((std::usize::MAX, std::usize::MIN), |acc, range| (min(acc.0, range.0), max(acc.1, range.1))); | ||
112 | |||
113 | let range = TextRange::from_to(TextUnit::from_usize(begin), TextUnit::from_usize(end)); | ||
114 | |||
115 | Some((range, comment_text)) | ||
116 | } | ||
117 | |||
64 | fn param_list(node: ast::FnDef) -> Vec<String> { | 118 | fn param_list(node: ast::FnDef) -> Vec<String> { |
65 | let mut res = vec![]; | 119 | let mut res = vec![]; |
66 | if let Some(param_list) = node.param_list() { | 120 | if let Some(param_list) = node.param_list() { |
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index f5683aec5..03e2df48e 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs | |||
@@ -185,6 +185,153 @@ fn bar() { | |||
185 | assert_eq!(param, Some(1)); | 185 | assert_eq!(param, Some(1)); |
186 | } | 186 | } |
187 | 187 | ||
188 | #[test] | ||
189 | fn test_fn_signature_with_docs_simple() { | ||
190 | let (desc, param) = get_signature( | ||
191 | r#" | ||
192 | // test | ||
193 | fn foo(j: u32) -> u32 { | ||
194 | j | ||
195 | } | ||
196 | |||
197 | fn bar() { | ||
198 | let _ = foo(<|>); | ||
199 | } | ||
200 | "#, | ||
201 | ); | ||
202 | |||
203 | assert_eq!(desc.name, "foo".to_string()); | ||
204 | assert_eq!(desc.params, vec!["j".to_string()]); | ||
205 | assert_eq!(desc.ret_type, Some("-> u32".to_string())); | ||
206 | assert_eq!(param, Some(0)); | ||
207 | assert_eq!(desc.label, "fn foo(j: u32) -> u32".to_string()); | ||
208 | assert_eq!(desc.doc, Some("test".into())); | ||
209 | } | ||
210 | |||
211 | #[test] | ||
212 | fn test_fn_signature_with_docs() { | ||
213 | let (desc, param) = get_signature( | ||
214 | r#" | ||
215 | /// Adds one to the number given. | ||
216 | /// | ||
217 | /// # Examples | ||
218 | /// | ||
219 | /// ``` | ||
220 | /// let five = 5; | ||
221 | /// | ||
222 | /// assert_eq!(6, my_crate::add_one(5)); | ||
223 | /// ``` | ||
224 | pub fn add_one(x: i32) -> i32 { | ||
225 | x + 1 | ||
226 | } | ||
227 | |||
228 | pub fn do() { | ||
229 | add_one(<|> | ||
230 | }"#, | ||
231 | ); | ||
232 | |||
233 | assert_eq!(desc.name, "add_one".to_string()); | ||
234 | assert_eq!(desc.params, vec!["x".to_string()]); | ||
235 | assert_eq!(desc.ret_type, Some("-> i32".to_string())); | ||
236 | assert_eq!(param, Some(0)); | ||
237 | assert_eq!(desc.label, "pub fn add_one(x: i32) -> i32".to_string()); | ||
238 | assert_eq!(desc.doc, Some( | ||
239 | r#"Adds one to the number given. | ||
240 | |||
241 | # Examples | ||
242 | |||
243 | ```rust | ||
244 | let five = 5; | ||
245 | |||
246 | assert_eq!(6, my_crate::add_one(5)); | ||
247 | ```"#.into())); | ||
248 | } | ||
249 | |||
250 | #[test] | ||
251 | fn test_fn_signature_with_docs_impl() { | ||
252 | let (desc, param) = get_signature( | ||
253 | r#" | ||
254 | struct addr; | ||
255 | impl addr { | ||
256 | /// Adds one to the number given. | ||
257 | /// | ||
258 | /// # Examples | ||
259 | /// | ||
260 | /// ``` | ||
261 | /// let five = 5; | ||
262 | /// | ||
263 | /// assert_eq!(6, my_crate::add_one(5)); | ||
264 | /// ``` | ||
265 | pub fn add_one(x: i32) -> i32 { | ||
266 | x + 1 | ||
267 | } | ||
268 | } | ||
269 | |||
270 | pub fn do_it() { | ||
271 | addr {}; | ||
272 | addr::add_one(<|>); | ||
273 | }"#); | ||
274 | |||
275 | assert_eq!(desc.name, "add_one".to_string()); | ||
276 | assert_eq!(desc.params, vec!["x".to_string()]); | ||
277 | assert_eq!(desc.ret_type, Some("-> i32".to_string())); | ||
278 | assert_eq!(param, Some(0)); | ||
279 | assert_eq!(desc.label, "pub fn add_one(x: i32) -> i32".to_string()); | ||
280 | assert_eq!(desc.doc, Some( | ||
281 | r#"Adds one to the number given. | ||
282 | |||
283 | # Examples | ||
284 | |||
285 | ```rust | ||
286 | let five = 5; | ||
287 | |||
288 | assert_eq!(6, my_crate::add_one(5)); | ||
289 | ```"#.into())); | ||
290 | } | ||
291 | |||
292 | #[test] | ||
293 | fn test_fn_signature_with_docs_from_actix() { | ||
294 | let (desc, param) = get_signature( | ||
295 | r#" | ||
296 | pub trait WriteHandler<E> | ||
297 | where | ||
298 | Self: Actor, | ||
299 | Self::Context: ActorContext, | ||
300 | { | ||
301 | /// Method is called when writer emits error. | ||
302 | /// | ||
303 | /// If this method returns `ErrorAction::Continue` writer processing | ||
304 | /// continues otherwise stream processing stops. | ||
305 | fn error(&mut self, err: E, ctx: &mut Self::Context) -> Running { | ||
306 | Running::Stop | ||
307 | } | ||
308 | |||
309 | /// Method is called when writer finishes. | ||
310 | /// | ||
311 | /// By default this method stops actor's `Context`. | ||
312 | fn finished(&mut self, ctx: &mut Self::Context) { | ||
313 | ctx.stop() | ||
314 | } | ||
315 | } | ||
316 | |||
317 | pub fn foo() { | ||
318 | WriteHandler r; | ||
319 | r.finished(<|>); | ||
320 | } | ||
321 | |||
322 | "#); | ||
323 | |||
324 | assert_eq!(desc.name, "finished".to_string()); | ||
325 | assert_eq!(desc.params, vec!["&mut self".to_string(), "ctx".to_string()]); | ||
326 | assert_eq!(desc.ret_type, None); | ||
327 | assert_eq!(param, Some(1)); | ||
328 | assert_eq!(desc.doc, Some( | ||
329 | r#"Method is called when writer finishes. | ||
330 | |||
331 | By default this method stops actor's `Context`."#.into())); | ||
332 | } | ||
333 | |||
334 | |||
188 | fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> { | 335 | fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> { |
189 | let (offset, code) = extract_offset(text); | 336 | let (offset, code) = extract_offset(text); |
190 | let code = code.as_str(); | 337 | let code = code.as_str(); |
diff --git a/crates/ra_editor/src/extend_selection.rs b/crates/ra_editor/src/extend_selection.rs index 9d8df25c3..71be15d39 100644 --- a/crates/ra_editor/src/extend_selection.rs +++ b/crates/ra_editor/src/extend_selection.rs | |||
@@ -10,7 +10,7 @@ pub fn extend_selection(file: &File, range: TextRange) -> Option<TextRange> { | |||
10 | extend(syntax.borrowed(), range) | 10 | extend(syntax.borrowed(), range) |
11 | } | 11 | } |
12 | 12 | ||
13 | pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> { | 13 | pub fn extend(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> { |
14 | if range.is_empty() { | 14 | if range.is_empty() { |
15 | let offset = range.start(); | 15 | let offset = range.start(); |
16 | let mut leaves = find_leaf_at_offset(root, offset); | 16 | let mut leaves = find_leaf_at_offset(root, offset); |
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 02a1b2d45..b98461ed2 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs | |||
@@ -20,7 +20,7 @@ mod typing; | |||
20 | pub use self::{ | 20 | pub use self::{ |
21 | code_actions::{add_derive, add_impl, flip_comma, introduce_variable, LocalEdit}, | 21 | code_actions::{add_derive, add_impl, flip_comma, introduce_variable, LocalEdit}, |
22 | edit::{Edit, EditBuilder}, | 22 | edit::{Edit, EditBuilder}, |
23 | extend_selection::extend_selection, | 23 | extend_selection::{extend, extend_selection}, |
24 | folding_ranges::{folding_ranges, Fold, FoldKind}, | 24 | folding_ranges::{folding_ranges, Fold, FoldKind}, |
25 | line_index::{LineCol, LineIndex}, | 25 | line_index::{LineCol, LineIndex}, |
26 | symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, | 26 | symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, |
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 4ac08e527..20cb5f772 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs | |||
@@ -5,7 +5,7 @@ use languageserver_types::{ | |||
5 | CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic, | 5 | CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic, |
6 | DiagnosticSeverity, DocumentSymbol, FoldingRange, FoldingRangeKind, FoldingRangeParams, | 6 | DiagnosticSeverity, DocumentSymbol, FoldingRange, FoldingRangeKind, FoldingRangeParams, |
7 | InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit, | 7 | InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit, |
8 | RenameParams, WorkspaceEdit, PrepareRenameResponse | 8 | RenameParams, WorkspaceEdit, PrepareRenameResponse, Documentation, MarkupContent, MarkupKind |
9 | }; | 9 | }; |
10 | use gen_lsp_server::ErrorCode; | 10 | use gen_lsp_server::ErrorCode; |
11 | use ra_analysis::{FileId, FoldKind, Query, RunnableKind}; | 11 | use ra_analysis::{FileId, FoldKind, Query, RunnableKind}; |
@@ -465,9 +465,18 @@ pub fn handle_signature_help( | |||
465 | }) | 465 | }) |
466 | .collect(); | 466 | .collect(); |
467 | 467 | ||
468 | let documentation = if let Some(doc) = descriptor.doc { | ||
469 | Some(Documentation::MarkupContent(MarkupContent { | ||
470 | kind: MarkupKind::Markdown, | ||
471 | value: doc | ||
472 | })) | ||
473 | } else { | ||
474 | None | ||
475 | }; | ||
476 | |||
468 | let sig_info = SignatureInformation { | 477 | let sig_info = SignatureInformation { |
469 | label: descriptor.label, | 478 | label: descriptor.label, |
470 | documentation: None, | 479 | documentation, |
471 | parameters: Some(parameters), | 480 | parameters: Some(parameters), |
472 | }; | 481 | }; |
473 | 482 | ||