Skip to content

Commit

Permalink
Add support for textDocument/documentHighlight request (#308)
Browse files Browse the repository at this point in the history
* Add support for textDocument/documentHighlight request

* Search for references only in the current file
  • Loading branch information
rapgenic authored Jun 4, 2024
1 parent 805ab71 commit 21c51b3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vhdl_lang/src/analysis/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,12 @@ impl DesignRoot {
searcher.references
}

pub fn find_all_references_in_source(&self, source: &Source, ent: EntRef) -> Vec<SrcPos> {
let mut searcher = FindAllReferences::new(self, ent);
let _ = self.search_source(source, &mut searcher);
searcher.references
}

pub fn public_symbols<'a>(&'a self) -> Box<dyn Iterator<Item = EntRef<'a>> + 'a> {
Box::new(self.libraries.values().flat_map(|library| {
std::iter::once(self.arenas.get(library.id)).chain(library.units.values().flat_map(
Expand Down
4 changes: 4 additions & 0 deletions vhdl_lang/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ impl Project {
self.root.find_all_references(ent)
}

pub fn find_all_references_in_source(&self, source: &Source, ent: &AnyEnt) -> Vec<SrcPos> {
self.root.find_all_references_in_source(source, ent)
}

/// Get source positions that are not resolved to a declaration
/// This is used for development to test where the language server is blind
pub fn find_all_unresolved(&self) -> (usize, Vec<SrcPos>) {
Expand Down
8 changes: 8 additions & 0 deletions vhdl_ls/src/stdio_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ impl ConnectionRpcChannel {
}
Err(request) => request,
};
let request = match extract::<request::DocumentHighlightRequest>(request) {
Ok((id, params)) => {
let result = server.document_highlight(&params.text_document_position_params);
self.send_response(lsp_server::Response::new_ok(id, result));
return;
}
Err(request) => request,
};
let request = match extract::<request::HoverRequest>(request) {
Ok((id, params)) => {
let result = server.text_document_hover(&params.text_document_position_params);
Expand Down
25 changes: 25 additions & 0 deletions vhdl_ls/src/vhdl_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ impl VHDLServer {
})),
workspace_symbol_provider: Some(OneOf::Left(true)),
document_symbol_provider: Some(OneOf::Left(true)),
document_highlight_provider: Some(OneOf::Left(true)),
completion_provider: Some(CompletionOptions {
resolve_provider: Some(true),
trigger_characters: Some(trigger_chars),
Expand Down Expand Up @@ -681,6 +682,30 @@ impl VHDLServer {
})
}

pub fn document_highlight(
&mut self,
params: &TextDocumentPositionParams,
) -> Option<Vec<DocumentHighlight>> {
let source = self
.project
.get_source(&uri_to_file_name(&params.text_document.uri))?;

let ent = self
.project
.find_declaration(&source, from_lsp_pos(params.position))?;

Some(
self.project
.find_all_references_in_source(&source, ent)
.iter()
.map(|pos| DocumentHighlight {
range: to_lsp_range(pos.range()),
kind: Some(DocumentHighlightKind::TEXT),
})
.collect(),
)
}

pub fn workspace_symbol(
&self,
params: &WorkspaceSymbolParams,
Expand Down

0 comments on commit 21c51b3

Please sign in to comment.