doc symbols

This commit is contained in:
Aleksey Kladov 2018-08-11 14:44:12 +03:00
parent 7afd84febc
commit f2291d6a76
11 changed files with 2436 additions and 2413 deletions

View File

@ -1,10 +1,10 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"out": true,
"node_modules": true
"out": true
// "node_modules": true
},
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
}
}
}

4721
code/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,7 @@
"postinstall": "node ./node_modules/vscode/bin/install"
},
"dependencies": {
"vscode-languageclient": "^4.3.0"
"vscode-languageclient": "^4.4.0"
},
"devDependencies": {
"@types/node": "^7.0.56",

View File

@ -10,7 +10,7 @@
};
use clap::{App, Arg, SubCommand};
use tools::collect_tests;
use libeditor::{File, syntax_tree, symbols};
use libeditor::{File, syntax_tree, file_symbols};
type Result<T> = ::std::result::Result<T, failure::Error>;
@ -51,7 +51,7 @@ fn main() -> Result<()> {
}
("symbols", _) => {
let file = file()?;
for s in symbols(&file) {
for s in file_symbols(&file) {
println!("{:?}", s);
}
}

View File

@ -7,7 +7,7 @@
use libsyntax2::{
ast::{self, NameOwner},
SyntaxNodeRef, AstNode,
AstNode,
algo::walk,
SyntaxKind::*,
};
@ -100,19 +100,6 @@ pub fn syntax_tree(file: &ast::File) -> String {
::libsyntax2::utils::dump_tree(&file.syntax())
}
pub fn symbols(file: &ast::File) -> Vec<Symbol> {
let syntax = file.syntax();
let res: Vec<Symbol> = walk::preorder(syntax.as_ref())
.filter_map(Declaration::cast)
.filter_map(|decl| {
let name = decl.name()?;
let range = decl.range();
Some(Symbol { name, range })
})
.collect();
res // NLL :-(
}
pub fn runnables(file: &ast::File) -> Vec<Runnable> {
file
.functions()
@ -134,27 +121,3 @@ pub fn runnables(file: &ast::File) -> Vec<Runnable> {
})
.collect()
}
struct Declaration<'f> (SyntaxNodeRef<'f>);
impl<'f> Declaration<'f> {
fn cast(node: SyntaxNodeRef<'f>) -> Option<Declaration<'f>> {
match node.kind() {
| STRUCT | ENUM | FUNCTION | TRAIT
| CONST_ITEM | STATIC_ITEM | MODULE | NAMED_FIELD
| TYPE_ITEM => Some(Declaration(node)),
_ => None
}
}
fn name(&self) -> Option<String> {
let name = self.0.children()
.find(|child| child.kind() == NAME)?;
Some(name.text())
}
fn range(&self) -> TextRange {
self.0.range()
}
}

View File

@ -5,7 +5,6 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
[dependencies]
failure = "0.1.2"
languageserver-types = "0.48.0"
serde_json = "1.0.24"
serde = "1.0.71"
serde_derive = "1.0.71"
@ -14,8 +13,13 @@ crossbeam-channel = "0.2.4"
threadpool = "1.7.1"
flexi_logger = "0.9.0"
log = "0.4.3"
url = "1.1.0"
url_serde = "0.2.0"
libsyntax2 = { path = "../libsyntax2" }
libeditor = { path = "../libeditor" }
libanalysis = { path = "../libanalysis" }
[dependencies.languageserver-types]
git = "https://github.com/matklad/languageserver-types"
rev = "70e6bf548b901f01dc249c20378d26dd4996c25f"

View File

@ -23,7 +23,7 @@
implementation_provider: None,
references_provider: None,
document_highlight_provider: None,
document_symbol_provider: None,
document_symbol_provider: Some(true),
workspace_symbol_provider: None,
code_action_provider: None,
code_lens_provider: None,

View File

@ -1,5 +1,5 @@
use url::Url;
use languageserver_types::{Range, Position, Diagnostic, DiagnosticSeverity};
use languageserver_types::{Range, Position, Diagnostic, DiagnosticSeverity, Url, DocumentSymbol, SymbolKind};
use libsyntax2::SyntaxKind;
use libanalysis::World;
use libeditor::{self, LineIndex, LineCol, TextRange, TextUnit};
@ -34,6 +34,50 @@ pub fn handle_extend_selection(
Ok(req::ExtendSelectionResult { selections })
}
pub fn handle_document_symbol(
world: World,
params: req::DocumentSymbolParams,
) -> Result<Option<req::DocumentSymbolResponse>> {
let path = params.text_document.file_path()?;
let file = world.file_syntax(&path)?;
let line_index = world.file_line_index(&path)?;
let mut res: Vec<DocumentSymbol> = Vec::new();
for symbol in libeditor::file_symbols(&file) {
let doc_symbol = DocumentSymbol {
name: symbol.name.clone(),
detail: Some(symbol.name),
kind: to_symbol_kind(symbol.kind),
deprecated: None,
range: to_vs_range(&line_index, symbol.node_range),
selection_range: to_vs_range(&line_index, symbol.name_range),
children: None,
};
if let Some(idx) = symbol.parent {
let children = &mut res[idx].children;
if children.is_none() {
*children = Some(Vec::new());
}
children.as_mut().unwrap().push(doc_symbol);
} else {
res.push(doc_symbol);
}
}
Ok(Some(req::DocumentSymbolResponse::Nested(res)))
}
fn to_symbol_kind(kind: SyntaxKind) -> SymbolKind {
match kind {
SyntaxKind::FUNCTION => SymbolKind::Function,
SyntaxKind::STRUCT => SymbolKind::Struct,
SyntaxKind::ENUM => SymbolKind::Enum,
SyntaxKind::TRAIT => SymbolKind::Interface,
SyntaxKind::MODULE => SymbolKind::Module,
_ => SymbolKind::Variable,
}
}
pub fn publish_diagnostics(world: World, uri: Url) -> Result<req::PublishDiagnosticsParams> {
let path = uri.file_path()?;
let file = world.file_syntax(&path)?;

View File

@ -11,11 +11,11 @@
extern crate threadpool;
#[macro_use]
extern crate log;
extern crate url;
extern crate url_serde;
extern crate flexi_logger;
extern crate libeditor;
extern crate libanalysis;
extern crate libsyntax2;
mod io;
mod caps;
@ -27,12 +27,13 @@
use threadpool::ThreadPool;
use crossbeam_channel::{bounded, Sender, Receiver};
use flexi_logger::Logger;
use url::Url;
use languageserver_types::Url;
use libanalysis::{WorldState, World};
use ::{
io::{Io, RawMsg, RawRequest},
handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics, publish_decorations},
handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics, publish_decorations,
handle_document_symbol},
util::{FilePath, FnBox}
};
@ -178,6 +179,9 @@ enum Event {
handle_request_on_threadpool::<req::ExtendSelection>(
&mut req, pool, world, &sender, handle_extend_selection
)?;
handle_request_on_threadpool::<req::DocumentSymbolRequest>(
&mut req, pool, world, &sender, handle_document_symbol
)?;
let mut shutdown = false;
dispatch::handle_request::<req::Shutdown, _>(&mut req, |(), resp| {
resp.result(io, ())?;

View File

@ -1,11 +1,11 @@
use serde::{ser::Serialize, de::DeserializeOwned};
use url::Url;
use languageserver_types::{TextDocumentIdentifier, Range};
use languageserver_types::{TextDocumentIdentifier, Range, Url};
use url_serde;
pub use languageserver_types::{
request::*, notification::*,
InitializeResult, PublishDiagnosticsParams,
DocumentSymbolParams, DocumentSymbolResponse
};

View File

@ -1,5 +1,6 @@
use std::path::PathBuf;
use languageserver_types::{TextDocumentItem, VersionedTextDocumentIdentifier, TextDocumentIdentifier};
use languageserver_types::{TextDocumentItem, VersionedTextDocumentIdentifier,
TextDocumentIdentifier, Url};
use ::{Result};
pub trait FnBox<A, R>: Send {
@ -34,7 +35,7 @@ fn file_path(&self) -> Result<PathBuf> {
}
}
impl FilePath for ::url::Url {
impl FilePath for Url {
fn file_path(&self) -> Result<PathBuf> {
self.to_file_path()
.map_err(|()| format_err!("invalid uri: {}", self))