Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mitigate race condition in completion list triggering #1682

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions language_service/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::compilation::{Compilation, CompilationKind};
use crate::protocol::{CompletionItem, CompletionItemKind, CompletionList, TextEdit};
use crate::qsc_utils::into_range;

use log::{log_enabled, trace, Level::Trace};
use qsc::ast::visit::{self, Visitor};
use qsc::display::{CodeDisplay, Lookup};

Expand All @@ -23,6 +24,7 @@ use std::rc::Rc;
type NamespaceName = Vec<Rc<str>>;
type NamespaceAlias = Rc<str>;

#[allow(clippy::too_many_lines)]
pub(crate) fn get_completions(
compilation: &Compilation,
source_name: &str,
Expand All @@ -33,6 +35,22 @@ pub(crate) fn get_completions(
compilation.source_position_to_package_offset(source_name, position, position_encoding);
let user_ast_package = &compilation.user_unit().ast.package;

if log_enabled!(Trace) {
let last_char = compilation
.user_unit()
.sources
.find_by_offset(offset)
.map(|s| {
let offset = offset - s.offset;
if offset > 0 {
s.contents[(offset as usize - 1)..].chars().next()
} else {
None
}
});
trace!("the character before the cursor is: {last_char:?}");
}

// Determine context for the offset
let mut context_finder = ContextFinder {
offset,
Expand Down
7 changes: 7 additions & 0 deletions npm/qsharp/src/language-service/language-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ export class QSharpLanguageService implements ILanguageService {
documentUri: string,
position: IPosition,
): Promise<ICompletionList> {
// Tiny delay to let the compilation catch up before we invoke
// the completion provider.
// This becomes important when the completion list is triggered
// during typing. If the last character typed is significant to
// the completion (e.g. in `Foo.` completions)
// it's critical that the completion provider "sees" this character.
await new Promise((resolve) => setTimeout(resolve, 50));
return this.languageService.get_completions(documentUri, position);
}

Expand Down
Loading