Fix regression when completing user environments

See #883.
This commit is contained in:
Patrick Förster 2023-04-22 14:02:56 +02:00
parent edee410a66
commit 4cc0b20a56
9 changed files with 50 additions and 16 deletions

View file

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Fixed
- Fix spurious completion results when completing environments ([#883](https://github.com/latex-lsp/texlab/issues/883))
## [5.5.0] - 2023-04-16
### Added

View file

@ -9,8 +9,8 @@ use super::Span;
pub struct Semantics {
pub links: Vec<Link>,
pub labels: Vec<Label>,
pub commands: Vec<(TextRange, String)>,
pub environments: FxHashSet<String>,
pub commands: Vec<Span>,
pub environments: Vec<Span>,
pub theorem_definitions: Vec<TheoremDefinition>,
pub graphics_paths: FxHashSet<String>,
pub can_be_root: bool,
@ -29,14 +29,12 @@ impl Semantics {
let range = token.text_range();
let range = TextRange::new(range.start() + "\\".text_len(), range.end());
let text = String::from(&token.text()[1..]);
self.commands.push((range, text));
self.commands.push(Span { range, text });
}
}
};
}
self.can_be_compiled = self.environments.contains("document");
self.can_be_root = self.can_be_compiled
&& !self
.links
@ -208,7 +206,9 @@ impl Semantics {
.and_then(|begin| begin.name())
.and_then(|group| group.key()) else { return };
self.environments.insert(String::from(name.syntax().text()));
let name = Span::from(&name);
self.can_be_compiled = self.can_be_compiled || name.text == "document";
self.environments.push(name);
}
fn process_theorem_definition(&mut self, theorem_def: latex::TheoremDefinition) {

View file

@ -6,7 +6,7 @@ pub fn complete<'db>(
context: &'db CursorContext,
builder: &mut CompletionBuilder<'db>,
) -> Option<()> {
let (_, range) = context.find_environment_name()?;
let range = context.find_environment_name()?;
for component in COMPONENT_DATABASE.linked_components(&context.project) {
for name in &component.environments {

View file

@ -8,7 +8,7 @@ pub fn complete<'db>(
context: &'db CursorContext,
builder: &mut CompletionBuilder<'db>,
) -> Option<()> {
let (_, range) = context.find_environment_name()?;
let range = context.find_environment_name()?;
for document in &context.project.documents {
let DocumentData::Tex(data) = &document.data else { continue };

View file

@ -12,8 +12,13 @@ pub fn complete<'db>(
for document in &context.project.documents {
let DocumentData::Tex(data) = &document.data else { continue };
for (_, name) in data.semantics.commands.iter().filter(|(r, _)| *r != range) {
builder.user_command(range, name);
for name in data
.semantics
.commands
.iter()
.filter(|name| name.range != range)
{
builder.user_command(range, &name.text);
}
}

View file

@ -8,7 +8,7 @@ pub fn complete<'db>(
context: &'db CursorContext,
builder: &mut CompletionBuilder<'db>,
) -> Option<()> {
let (name, range) = context.find_environment_name()?;
let range = context.find_environment_name()?;
for document in &context.project.documents {
let DocumentData::Tex(data) = &document.data else { continue };
@ -16,9 +16,9 @@ pub fn complete<'db>(
.semantics
.environments
.iter()
.filter(|n| n.as_str() != name)
.filter(|name| name.range != range)
{
builder.user_environment(range, name);
builder.user_environment(range, &name.text);
}
}

View file

@ -229,14 +229,14 @@ impl<'a, T> CursorContext<'a, T> {
}
}
pub fn find_environment_name(&self) -> Option<(String, TextRange)> {
let (name, range, group) = self.find_curly_group_word()?;
pub fn find_environment_name(&self) -> Option<TextRange> {
let (_, range, group) = self.find_curly_group_word()?;
if !matches!(group.syntax().parent()?.kind(), latex::BEGIN | latex::END) {
return None;
}
Some((name, range))
Some(range)
}
pub fn find_environment(&self) -> Option<(latex::Key, latex::Key)> {

View file

@ -852,3 +852,15 @@ fn issue_864() {
\end{document}"#
))
}
#[test]
fn issue_883() {
assert_json_snapshot!(complete(
r#"
%! bug.tex
\begin{doc
|
^^^
% Comment"#
))
}

View file

@ -0,0 +1,11 @@
---
source: crates/texlab/tests/lsp/text_document/completion.rs
expression: "complete(r#\"\n%! bug.tex\n\\begin{doc\n |\n ^^^\n% Comment\"#)"
---
[
{
"label": "document",
"detail": "built-in",
"preselect": false
}
]