Skip to content

Commit

Permalink
feat: optimize ICompletionItem type and add ICompletionList type (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaydenOrz authored Jan 30, 2024
1 parent 7567b5b commit 00c7c6b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
23 changes: 16 additions & 7 deletions src/_.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { languages, Emitter, IEvent, editor, Position } from './fillers/monaco-editor-core';
import { languages, Emitter, IEvent, editor, Position, IRange } from './fillers/monaco-editor-core';
import { Suggestions } from 'dt-sql-parser';

interface ILang extends languages.ILanguageExtensionPoint {
Expand Down Expand Up @@ -146,12 +146,21 @@ export interface DiagnosticsOptions {
}

/**
* A completion item, it will be convert to monaco.languages.CompletionItem.
* A completion item.
* ICompletionItem is pretty much the same as {@link languages.CompletionItem},
* with the only difference being that the range and insertText is optional.
*/
export interface ICompletionItem extends Partial<languages.CompletionItem> {
label: string | languages.CompletionItemLabel;
kind: languages.CompletionItemKind;
detail: string;
export interface ICompletionItem extends Omit<languages.CompletionItem, 'range' | 'insertText'> {
range?: IRange | languages.CompletionItemRanges;
insertText?: string;
}

/**
* ICompletionList is pretty much the same as {@link languages.CompletionList},
* with the only difference being that the type of suggestion is {@link ICompletionItem}
*/
export interface ICompletionList extends Omit<languages.CompletionList, 'suggestions'> {
suggestions: ICompletionItem[];
}

/**
Expand All @@ -166,7 +175,7 @@ export type CompletionService = (
position: Position,
completionContext: languages.CompletionContext,
suggestions: Suggestions | null
) => Promise<ICompletionItem[] | { completionItems: ICompletionItem[], incomplete: boolean }>;
) => Promise<ICompletionItem[] | ICompletionList>;

export interface LanguageServiceDefaults {
readonly languageId: string;
Expand Down
27 changes: 16 additions & 11 deletions src/languageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,25 @@ export class CompletionAdapter<T extends IWorker> implements languages.Completio
position.lineNumber,
wordInfo.endColumn
);
const unwrappedCompletions = Array.isArray(completions) ? completions : completions.completionItems
const completionItems: languages.CompletionItem[] = unwrappedCompletions.map((item) => ({
...item,
insertText:
item.insertText ??
(typeof item.label === 'string' ? item.label : item.label.label),
range: item.range ?? wordRange,
insertTextRules:
item.insertTextRules ??
languages.CompletionItemInsertTextRule.InsertAsSnippet
}));
const unwrappedCompletions = Array.isArray(completions)
? completions
: completions.suggestions;
const completionItems: languages.CompletionItem[] = unwrappedCompletions.map(
(item) => ({
...item,
insertText:
item.insertText ??
(typeof item.label === 'string' ? item.label : item.label.label),
range: item.range ?? wordRange,
insertTextRules:
item.insertTextRules ??
languages.CompletionItemInsertTextRule.InsertAsSnippet
})
);

return {
suggestions: completionItems,
dispose: Array.isArray(completions) ? undefined : completions.dispose,
incomplete: Array.isArray(completions) ? undefined : completions.incomplete
};
});
Expand Down

0 comments on commit 00c7c6b

Please sign in to comment.