mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
doc symbols
This commit is contained in:
parent
7afd84febc
commit
f2291d6a76
11 changed files with 2436 additions and 2413 deletions
|
@ -23,7 +23,7 @@ pub const SERVER_CAPABILITIES: ServerCapabilities = ServerCapabilities {
|
|||
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,
|
||||
|
|
|
@ -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)?;
|
||||
|
|
|
@ -11,11 +11,11 @@ extern crate crossbeam_channel;
|
|||
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 @@ mod util;
|
|||
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 @@ fn main_loop(
|
|||
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, ())?;
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -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 @@ impl FilePath for TextDocumentIdentifier {
|
|||
}
|
||||
}
|
||||
|
||||
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))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue