mirror of
https://github.com/google/gn-language-server.git
synced 2025-12-23 12:26:43 +00:00
Simplify SymbolSet
This commit is contained in:
parent
a7ffb75ed6
commit
71b93e4d8b
3 changed files with 63 additions and 80 deletions
|
|
@ -17,7 +17,7 @@ use std::{collections::HashMap, path::Path};
|
|||
use itertools::Itertools;
|
||||
use tower_lsp::lsp_types::{
|
||||
CodeAction, CodeActionKind, CodeActionOrCommand, CodeActionParams, CodeActionResponse, Command,
|
||||
Diagnostic, NumberOrString, SymbolKind, Url, WorkspaceEdit,
|
||||
Diagnostic, NumberOrString, Url, WorkspaceEdit,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
|
@ -53,14 +53,11 @@ async fn compute_import_actions(
|
|||
let symbols = SymbolSet::workspace(&workspace).await;
|
||||
|
||||
let imports: Vec<String> = symbols
|
||||
.symbol_informations()
|
||||
.filter(|symbol| {
|
||||
symbol.name == name
|
||||
&& matches!(symbol.kind, SymbolKind::VARIABLE | SymbolKind::CONSTANT)
|
||||
})
|
||||
.map(|symbol| {
|
||||
.variables()
|
||||
.filter(|variable| variable.name == name)
|
||||
.map(|variable| {
|
||||
format_path(
|
||||
&symbol.location.uri.to_file_path().unwrap(),
|
||||
&variable.assignments.first().unwrap().document.path,
|
||||
&workspace.context().root,
|
||||
)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -12,24 +12,75 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use tower_lsp::lsp_types::{SymbolInformation, WorkspaceSymbolParams};
|
||||
use either::Either;
|
||||
use tower_lsp::lsp_types::{Location, SymbolInformation, SymbolKind, Url, WorkspaceSymbolParams};
|
||||
|
||||
use crate::{
|
||||
analyzer::{Template, Variable},
|
||||
common::error::Result,
|
||||
server::{symbols::SymbolSet, RequestContext},
|
||||
};
|
||||
|
||||
impl Variable<'_> {
|
||||
#[allow(deprecated)]
|
||||
fn as_symbol_information(&self) -> SymbolInformation {
|
||||
let first_assignment = self.assignments.first().unwrap();
|
||||
SymbolInformation {
|
||||
name: self.name.to_string(),
|
||||
kind: if self.is_args {
|
||||
SymbolKind::CONSTANT
|
||||
} else {
|
||||
SymbolKind::VARIABLE
|
||||
},
|
||||
tags: None,
|
||||
deprecated: None,
|
||||
location: Location {
|
||||
uri: Url::from_file_path(&first_assignment.document.path).unwrap(),
|
||||
range: first_assignment.document.line_index.range(
|
||||
match first_assignment.assignment_or_call {
|
||||
Either::Left(assignment) => assignment.span,
|
||||
Either::Right(call) => call.span,
|
||||
},
|
||||
),
|
||||
},
|
||||
container_name: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Template<'_> {
|
||||
#[allow(deprecated)]
|
||||
fn as_symbol_information(&self) -> SymbolInformation {
|
||||
SymbolInformation {
|
||||
name: self.name.to_string(),
|
||||
kind: SymbolKind::FUNCTION,
|
||||
tags: None,
|
||||
deprecated: None,
|
||||
location: Location {
|
||||
uri: Url::from_file_path(&self.document.path).unwrap(),
|
||||
range: self.document.line_index.range(self.call.span),
|
||||
},
|
||||
container_name: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn workspace_symbol(
|
||||
context: &RequestContext,
|
||||
params: WorkspaceSymbolParams,
|
||||
) -> Result<Option<Vec<SymbolInformation>>> {
|
||||
let symbols = SymbolSet::global(&context.analyzer).await;
|
||||
|
||||
let query = params.query.to_lowercase();
|
||||
let symbols = symbols
|
||||
.symbol_informations()
|
||||
.filter(|symbol| symbol.name.starts_with(&query))
|
||||
.collect();
|
||||
let query = params.query.to_ascii_lowercase();
|
||||
|
||||
Ok(Some(symbols))
|
||||
let variable_symbols = symbols
|
||||
.variables()
|
||||
.filter(|variable| variable.name.to_ascii_lowercase().starts_with(&query))
|
||||
.map(|variable| variable.as_symbol_information());
|
||||
let template_symbols = symbols
|
||||
.templates()
|
||||
.filter(|template| template.name.to_ascii_lowercase().starts_with(&query))
|
||||
.map(|template| template.as_symbol_information());
|
||||
|
||||
Ok(Some(variable_symbols.chain(template_symbols).collect()))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,55 +14,8 @@
|
|||
|
||||
use std::sync::Arc;
|
||||
|
||||
use either::Either;
|
||||
use tower_lsp::lsp_types::{Location, SymbolInformation, SymbolKind, Url};
|
||||
|
||||
use crate::analyzer::{AnalyzedFile, Analyzer, Template, Variable, WorkspaceAnalyzer};
|
||||
|
||||
impl Variable<'_> {
|
||||
#[allow(deprecated)]
|
||||
fn as_symbol_information(&self) -> SymbolInformation {
|
||||
let first_assignment = self.assignments.first().unwrap();
|
||||
SymbolInformation {
|
||||
name: self.name.to_string(),
|
||||
kind: if self.is_args {
|
||||
SymbolKind::CONSTANT
|
||||
} else {
|
||||
SymbolKind::VARIABLE
|
||||
},
|
||||
tags: None,
|
||||
deprecated: None,
|
||||
location: Location {
|
||||
uri: Url::from_file_path(&first_assignment.document.path).unwrap(),
|
||||
range: first_assignment.document.line_index.range(
|
||||
match first_assignment.assignment_or_call {
|
||||
Either::Left(assignment) => assignment.span,
|
||||
Either::Right(call) => call.span,
|
||||
},
|
||||
),
|
||||
},
|
||||
container_name: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Template<'_> {
|
||||
#[allow(deprecated)]
|
||||
fn as_symbol_information(&self) -> SymbolInformation {
|
||||
SymbolInformation {
|
||||
name: self.name.to_string(),
|
||||
kind: SymbolKind::FUNCTION,
|
||||
tags: None,
|
||||
deprecated: None,
|
||||
location: Location {
|
||||
uri: Url::from_file_path(&self.document.path).unwrap(),
|
||||
range: self.document.line_index.range(self.call.span),
|
||||
},
|
||||
container_name: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SymbolSet {
|
||||
files: Vec<Arc<AnalyzedFile>>,
|
||||
}
|
||||
|
|
@ -95,24 +48,6 @@ impl SymbolSet {
|
|||
Self { files }
|
||||
}
|
||||
|
||||
pub fn symbol_informations(&self) -> impl Iterator<Item = SymbolInformation> + '_ {
|
||||
self.files.iter().flat_map(|file| {
|
||||
let variables = file
|
||||
.exports
|
||||
.get()
|
||||
.variables
|
||||
.values()
|
||||
.map(|variable| variable.as_symbol_information());
|
||||
let templates = file
|
||||
.exports
|
||||
.get()
|
||||
.templates
|
||||
.values()
|
||||
.map(|template| template.as_symbol_information());
|
||||
variables.chain(templates)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn variables(&self) -> impl Iterator<Item = &Variable<'_>> + '_ {
|
||||
self.files
|
||||
.iter()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue