Skip to content

Commit

Permalink
Avoid unnecessary duplicates in Completion Lists (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottCarda-MS authored Jan 9, 2024
1 parent 0cabbc7 commit 0a77b2d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
7 changes: 4 additions & 3 deletions language_service/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::qsc_utils::{protocol_span, span_contains};
use qsc::ast::visit::{self, Visitor};
use qsc::hir::{ItemKind, Package, PackageId};
use qsc::resolve::{Local, LocalKind};
use rustc_hash::FxHashSet;
use std::rc::Rc;

const PRELUDE: [&str; 3] = [
Expand Down Expand Up @@ -157,19 +158,19 @@ fn get_indent(compilation: &Compilation, package_offset: u32) -> String {

struct CompletionListBuilder {
current_sort_group: u32,
items: Vec<CompletionItem>,
items: FxHashSet<CompletionItem>,
}

impl CompletionListBuilder {
fn new() -> Self {
CompletionListBuilder {
current_sort_group: 1,
items: Vec::new(),
items: FxHashSet::default(),
}
}

fn into_items(self) -> Vec<CompletionItem> {
self.items
self.items.into_iter().collect()
}

fn push_item_decl_keywords(&mut self) {
Expand Down
28 changes: 26 additions & 2 deletions language_service/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct WorkspaceConfigurationUpdate {
}

/// Represents a span of text used by the Language Server API
#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct Span {
pub start: u32,
pub end: u32,
Expand All @@ -24,7 +24,7 @@ pub struct DiagnosticUpdate {
pub errors: Vec<Error>,
}

#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[allow(clippy::module_name_repetitions)]
pub enum CompletionItemKind {
// It would have been nice to match the numeric values to the ones used by
Expand Down Expand Up @@ -68,6 +68,30 @@ impl CompletionItem {
}
}

impl PartialEq for CompletionItem {
// exclude sort text for comparison
fn eq(&self, other: &Self) -> bool {
self.label == other.label
&& self.kind == other.kind
&& self.detail == other.detail
&& self.additional_text_edits == other.additional_text_edits
}
}

impl Eq for CompletionItem {}

use std::hash::{Hash, Hasher};

impl Hash for CompletionItem {
// exclude sort text for hashing
fn hash<H: Hasher>(&self, state: &mut H) {
self.label.hash(state);
self.kind.hash(state);
self.detail.hash(state);
self.additional_text_edits.hash(state);
}
}

#[derive(Debug, PartialEq)]
pub struct Location {
pub source: String,
Expand Down

0 comments on commit 0a77b2d

Please sign in to comment.