Skip to content

Commit

Permalink
Merge branch 'master' into restructure-language-server
Browse files Browse the repository at this point in the history
  • Loading branch information
Schottkyc137 committed Jun 10, 2024
2 parents 348e42d + 21c51b3 commit 5675187
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 14 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
2 changes: 1 addition & 1 deletion vhdl_lang/src/analysis/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl<'a, 't> AnalyzeContext<'a, 't> {
&mut elem_decl.ident,
type_ent.into(),
AnyEntKind::ElementDeclaration(subtype),
src_span,
elem_decl.span,
Some(self.source()),
);
region.add(elem, diagnostics);
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
18 changes: 10 additions & 8 deletions vhdl_lang/src/syntax/type_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::ast::*;
use crate::ast::{AbstractLiteral, Range};
use crate::named_entity::Reference;
use crate::syntax::names::parse_type_mark;
use crate::HasTokenSpan;
use vhdl_lang::syntax::parser::ParsingContext;

/// LRM 5.2.2 Enumeration types
Expand Down Expand Up @@ -81,18 +80,19 @@ fn parse_record_type_definition(
return Ok((TypeDefinition::Record(elem_decls), end_ident));
};

let start_token = ctx.stream.get_current_token_id();

let idents = parse_identifier_list(ctx)?;
ctx.stream.expect_kind(Colon)?;
let subtype = parse_subtype_indication(ctx)?;
let end_token = ctx.stream.expect_kind(SemiColon)?;
for ident in idents {
let ident_span = ident.token.span();
elem_decls.push(ElementDeclaration {
ident: ident.into(),
subtype: subtype.clone(),
span: ident_span,
span: TokenSpan::new(start_token, end_token),
});
}
ctx.stream.expect_kind(SemiColon)?;
}
}

Expand Down Expand Up @@ -531,7 +531,7 @@ end record;",
let elem_decl = ElementDeclaration {
ident: code.s1("element").decl_ident(),
subtype: code.s1("boolean").subtype_indication(),
span: code.s1("element").token_span(),
span: code.s1("element : boolean;").token_span(),
};

let type_decl = TypeDeclaration {
Expand Down Expand Up @@ -560,19 +560,21 @@ end foo;",
let elem_decl0a = ElementDeclaration {
ident: code.s1("element").decl_ident(),
subtype: code.s1("boolean").subtype_indication(),
span: code.s1("element").token_span(),
span: code.s1("element, field : boolean;").token_span(),
};

let elem_decl0b = ElementDeclaration {
ident: code.s1("field").decl_ident(),
subtype: code.s1("boolean").subtype_indication(),
span: code.s1("field").token_span(),
span: code.s1("element, field : boolean;").token_span(),
};

let elem_decl1 = ElementDeclaration {
ident: code.s1("other_element").decl_ident(),
subtype: code.s1("std_logic_vector(0 to 1)").subtype_indication(),
span: code.s1("other_element").token_span(),
span: code
.s1("other_element : std_logic_vector(0 to 1);")
.token_span(),
};

let type_decl = TypeDeclaration {
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
16 changes: 16 additions & 0 deletions vhdl_ls/src/vhdl_server/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ use serde_json::Value;
use vhdl_lang::{Message, Project};

impl VHDLServer {
fn apply_initial_options(&mut self, options: &Value) {
let Some(non_project_file_handling) = options.get("nonProjectFiles") else {
return;
};
match non_project_file_handling {
Value::String(handling) => match NonProjectFileHandling::from_string(handling) {
None => self.message(Message::error(format!(
"Illegal setting {handling} for nonProjectFiles setting"
))),
Some(handling) => self.settings.non_project_file_handling = handling,
},
_ => self.message(Message::error("nonProjectFiles must be a string")),
}
}

/// Register capabilities on the client side:
/// - watch workspace config file for changes
fn register_capabilities(&mut self) {
Expand Down Expand Up @@ -57,6 +72,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
34 changes: 29 additions & 5 deletions vhdl_ls/src/vhdl_server/text_document.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::vhdl_server::{
from_lsp_pos, from_lsp_range, srcpos_to_location, uri_to_file_name, NonProjectFileHandling,
VHDLServer,
from_lsp_pos, from_lsp_range, srcpos_to_location, to_lsp_range, uri_to_file_name,
NonProjectFileHandling, VHDLServer,
};
use lsp_types::{
DidChangeTextDocumentParams, DidOpenTextDocumentParams, GotoDefinitionResponse, Hover,
HoverContents, Location, MarkupContent, MarkupKind, ReferenceParams, TextDocumentItem,
TextDocumentPositionParams,
DidChangeTextDocumentParams, DidOpenTextDocumentParams, DocumentHighlight,
DocumentHighlightKind, GotoDefinitionResponse, Hover, HoverContents, Location, MarkupContent,
MarkupKind, ReferenceParams, TextDocumentItem, TextDocumentPositionParams,
};
use vhdl_lang::{Message, Source};

Expand Down Expand Up @@ -139,4 +139,28 @@ impl VHDLServer {
Vec::new()
}
}

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(),
)
}
}

0 comments on commit 5675187

Please sign in to comment.