Skip to content

Commit

Permalink
feat: document symbols support
Browse files Browse the repository at this point in the history
  • Loading branch information
viddrobnic committed Aug 19, 2024
1 parent 3e0ceda commit 21ba624
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion language_server/src/analyze/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use symbol_table::SymbolTable;

pub mod document_info;
pub mod location;
pub mod symbol_info;

mod documentation;
mod symbol_info;
mod symbol_table;

pub fn analyze(program: &ast::Program) -> DocumentInfo {
Expand Down
2 changes: 1 addition & 1 deletion language_server/src/analyze/symbol_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use parser::position::Range;

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum DocumentSymbolKind {
Function = 12,
Variable = 13,
Expand Down
14 changes: 14 additions & 0 deletions language_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{

use analyze::{analyze, document_info::DocumentInfo};
use diagnostics::{Diagnostic, DiagnosticSeverity, PublishDiagnosticsParams};
use document_symbol::{DocumentSymbol, DocumentSymbolParams};
use error::{Error, ErrorKind};
use hover::{Hover, MarkupContent, MarkupKind};
use message::{initialize::*, *};
Expand Down Expand Up @@ -266,6 +267,18 @@ impl Server {

Response::new_ok(req_id, res)
}
"textDocument/documentSymbol" => {
let (req_id, params) = req.extract::<DocumentSymbolParams>()?;
let doc_name = params.text_document.uri.clone();

let doc_info = self.documents.get(&doc_name);
let mut res: Option<Vec<DocumentSymbol>> = None;
if let Some(doc_info) = doc_info {
res = Some(DocumentSymbol::map_tree(&doc_info.symbol_tree));
}

Response::new_ok(req_id, res)
}

method => {
self.log(LogLevel::Warn, &format!("Got unknown method: {method}"));
Expand Down Expand Up @@ -295,6 +308,7 @@ impl Server {
document_highlight_provider: true,
references_provider: true,
hover_provider: true,
document_symbol_provider: true,
},
}
}
Expand Down
40 changes: 40 additions & 0 deletions language_server/src/message/document_symbol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use parser::position::Range;
use serde::{Deserialize, Serialize};

use crate::{analyze::symbol_info, TextDocumentIdentifier};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DocumentSymbolParams {
pub text_document: TextDocumentIdentifier,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DocumentSymbol {
pub name: String,
pub kind: u8,
pub range: Range,
pub selection_range: Range,
pub children: Vec<DocumentSymbol>,
}

impl DocumentSymbol {
pub fn map_tree(tree: &[symbol_info::DocumentSymbol]) -> Vec<DocumentSymbol> {
tree.iter()
.filter_map(|sym| {
let Some(name) = &sym.name else {
return None;
};

Some(DocumentSymbol {
name: name.clone(),
kind: sym.kind as u8,
range: sym.range,
selection_range: sym.name_range,
children: DocumentSymbol::map_tree(&sym.children),
})
})
.collect()
}
}
1 change: 1 addition & 0 deletions language_server/src/message/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct ServerCapabilities {
pub document_highlight_provider: bool,
pub references_provider: bool,
pub hover_provider: bool,
pub document_symbol_provider: bool,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions language_server/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde_json::Value;
use crate::error::{Error, ErrorKind};

pub mod diagnostics;
pub mod document_symbol;
pub mod hover;
pub mod initialize;
pub mod reference;
Expand Down

0 comments on commit 21ba624

Please sign in to comment.