UNPKG

11.9 kBTypeScriptView Raw
1import { ActiveServer } from "../server-manager";
2import { CompletionContext, CompletionItem, CompletionList, CompletionParams, InsertReplaceEdit, ServerCapabilities, TextEdit } from "../languageclient";
3import { Point, TextEditor } from "atom";
4import * as ac from "atom/autocomplete-plus";
5import { Suggestion, TextSuggestion, SnippetSuggestion } from "../types/autocomplete-extended";
6/**
7 * Defines the behavior of suggestion acceptance. Assume you have "cons|ole" in the editor ( `|` is the cursor position)
8 * and the autocomplete suggestion is `const`.
9 *
10 * - If `false` -> the edits are inserted : const|ole
11 * - If `true`` -> the edits are replaced: const|
12 */
13declare type ShouldReplace = boolean;
14declare type CompletionItemAdjuster = (item: CompletionItem, suggestion: ac.AnySuggestion, request: ac.SuggestionsRequestedEvent) => void;
15declare class PossiblyResolvedCompletionItem {
16 completionItem: CompletionItem;
17 isResolved: boolean;
18 constructor(completionItem: CompletionItem, isResolved: boolean);
19}
20/** Public: Adapts the language server protocol "textDocument/completion" to the Atom AutoComplete+ package. */
21export default class AutocompleteAdapter {
22 static canAdapt(serverCapabilities: ServerCapabilities): boolean;
23 static canResolve(serverCapabilities: ServerCapabilities): boolean;
24 private _suggestionCache;
25 private _cancellationTokens;
26 /**
27 * Public: Obtain suggestion list for AutoComplete+ by querying the language server using the `textDocument/completion` request.
28 *
29 * @param server An {ActiveServer} pointing to the language server to query.
30 * @param request The {atom$AutocompleteRequest} to satisfy.
31 * @param onDidConvertCompletionItem An optional function that takes a {CompletionItem}, an
32 * {atom$AutocompleteSuggestion} and a {atom$AutocompleteRequest} allowing you to adjust converted items.
33 * @param shouldReplace The behavior of suggestion acceptance (see {ShouldReplace}).
34 * @returns A {Promise} of an {Array} of {atom$AutocompleteSuggestion}s containing the AutoComplete+ suggestions to display.
35 */
36 getSuggestions(server: ActiveServer, request: ac.SuggestionsRequestedEvent, onDidConvertCompletionItem?: CompletionItemAdjuster, minimumWordLength?: number, shouldReplace?: ShouldReplace): Promise<ac.AnySuggestion[]>;
37 private shouldTrigger;
38 private getOrBuildSuggestions;
39 /**
40 * Public: Obtain a complete version of a suggestion with additional information the language server can provide by
41 * way of the `completionItem/resolve` request.
42 *
43 * @param server An {ActiveServer} pointing to the language server to query.
44 * @param suggestion An {atom$AutocompleteSuggestion} suggestion that should be resolved.
45 * @param request An {Object} with the AutoComplete+ request to satisfy.
46 * @param onDidConvertCompletionItem An optional function that takes a {CompletionItem}, an
47 * {atom$AutocompleteSuggestion} and a {atom$AutocompleteRequest} allowing you to adjust converted items.
48 * @returns A {Promise} of an {atom$AutocompleteSuggestion} with the resolved AutoComplete+ suggestion.
49 */
50 completeSuggestion(server: ActiveServer, suggestion: ac.AnySuggestion, request: ac.SuggestionsRequestedEvent, onDidConvertCompletionItem?: CompletionItemAdjuster): Promise<ac.AnySuggestion>;
51 static resolveSuggestion(resolvedCompletionItem: CompletionItem, suggestion: ac.AnySuggestion, request: ac.SuggestionsRequestedEvent, onDidConvertCompletionItem?: CompletionItemAdjuster): void;
52 /**
53 * Public: Get the trigger character that caused the autocomplete (if any). This is required because AutoComplete-plus
54 * does not have trigger characters. Although the terminology is 'character' we treat them as variable length strings
55 * as this will almost certainly change in the future to support '->' etc.
56 *
57 * @param request An {Array} of {atom$AutocompleteSuggestion}s to locate the prefix, editor, bufferPosition etc.
58 * @param triggerChars The {Array} of {string}s that can be trigger characters.
59 * @returns A [{string}, boolean] where the string is the matching trigger character or an empty string if one was not
60 * matched, and the boolean is true if the trigger character is in request.prefix, and false if it is in the word
61 * before request.prefix. The boolean return value has no meaning if the string return value is an empty string.
62 */
63 static getTriggerCharacter(request: ac.SuggestionsRequestedEvent, triggerChars: string[]): [string, boolean];
64 /**
65 * Public: Create TextDocumentPositionParams to be sent to the language server based on the editor and position from
66 * the AutoCompleteRequest.
67 *
68 * @param request The {atom$AutocompleteRequest} to obtain the editor from.
69 * @param triggerPoint The {atom$Point} where the trigger started.
70 * @returns A {string} containing the prefix including the trigger character.
71 */
72 static getPrefixWithTrigger(request: ac.SuggestionsRequestedEvent, triggerPoint: Point): string;
73 /**
74 * Public: Create {CompletionParams} to be sent to the language server based on the editor and position from the
75 * Autocomplete request etc.
76 *
77 * @param request The {atom$AutocompleteRequest} containing the request details.
78 * @param triggerCharacter The {string} containing the trigger character (empty if none).
79 * @param triggerOnly A {boolean} representing whether this completion is triggered right after a trigger character.
80 * @returns A {CompletionParams} with the keys:
81 *
82 * - `textDocument` the language server protocol textDocument identification.
83 * - `position` the position within the text document to display completion request for.
84 * - `context` containing the trigger character and kind.
85 */
86 static createCompletionParams(request: ac.SuggestionsRequestedEvent, triggerCharacter: string, triggerOnly: boolean): CompletionParams;
87 /**
88 * Public: Create {CompletionContext} to be sent to the language server based on the trigger character.
89 *
90 * @param triggerCharacter The {string} containing the trigger character or '' if none.
91 * @param triggerOnly A {boolean} representing whether this completion is triggered right after a trigger character.
92 * @returns An {CompletionContext} that specifies the triggerKind and the triggerCharacter if there is one.
93 */
94 static createCompletionContext(triggerCharacter: string, triggerOnly: boolean): CompletionContext;
95 /**
96 * Public: Convert a language server protocol CompletionItem array or CompletionList to an array of ordered
97 * AutoComplete+ suggestions.
98 *
99 * @param completionItems An {Array} of {CompletionItem} objects or a {CompletionList} containing completion items to
100 * be converted.
101 * @param request The {atom$AutocompleteRequest} to satisfy.
102 * @param shouldReplace The behavior of suggestion acceptance (see {ShouldReplace}).
103 * @param onDidConvertCompletionItem A function that takes a {CompletionItem}, an {atom$AutocompleteSuggestion} and a
104 * {atom$AutocompleteRequest} allowing you to adjust converted items.
105 * @returns A {Map} of AutoComplete+ suggestions ordered by the CompletionItems sortText.
106 */
107 completionItemsToSuggestions(completionItems: CompletionItem[] | CompletionList | null, request: ac.SuggestionsRequestedEvent, triggerColumns: [number, number], shouldReplace: ShouldReplace, onDidConvertCompletionItem?: CompletionItemAdjuster): Map<Suggestion, PossiblyResolvedCompletionItem>;
108 /**
109 * Public: Convert a language server protocol CompletionItem to an AutoComplete+ suggestion.
110 *
111 * @param item An {CompletionItem} containing a completion item to be converted.
112 * @param suggestion A {atom$AutocompleteSuggestion} to have the conversion applied to.
113 * @param request The {atom$AutocompleteRequest} to satisfy.
114 * @param shouldReplace The behavior of suggestion acceptance (see {ShouldReplace}).
115 * @param onDidConvertCompletionItem A function that takes a {CompletionItem}, an {atom$AutocompleteSuggestion} and a
116 * {atom$AutocompleteRequest} allowing you to adjust converted items.
117 * @returns The {atom$AutocompleteSuggestion} passed in as suggestion with the conversion applied.
118 */
119 static completionItemToSuggestion(item: CompletionItem, suggestion: Suggestion, request: ac.SuggestionsRequestedEvent, triggerColumns: [number, number], shouldReplace: ShouldReplace, onDidConvertCompletionItem?: CompletionItemAdjuster): Suggestion;
120 /**
121 * Public: Convert the primary parts of a language server protocol CompletionItem to an AutoComplete+ suggestion.
122 *
123 * @param item An {CompletionItem} containing the completion items to be merged into.
124 * @param suggestion The {Suggestion} to merge the conversion into.
125 * @returns The {Suggestion} with details added from the {CompletionItem}.
126 */
127 static applyCompletionItemToSuggestion(item: CompletionItem, suggestion: TextSuggestion): void;
128 static applyDetailsToSuggestion(item: CompletionItem, suggestion: Suggestion): void;
129 /**
130 * Public: Applies the textEdit part of a language server protocol CompletionItem to an AutoComplete+ Suggestion via
131 * the replacementPrefix and text properties.
132 *
133 * @param textEdit A {TextEdit} from a CompletionItem to apply.
134 * @param editor An Atom {TextEditor} used to obtain the necessary text replacement.
135 * @param suggestion An {atom$AutocompleteSuggestion} to set the replacementPrefix and text properties of.
136 * @param shouldReplace The behavior of suggestion acceptance (see {ShouldReplace}).
137 */
138 static applyTextEditToSuggestion(textEdit: TextEdit | InsertReplaceEdit | undefined, editor: TextEditor, triggerColumns: [number, number], originalBufferPosition: Point, suggestion: TextSuggestion, shouldReplace: ShouldReplace): void;
139 /**
140 * Handle additional text edits after a suggestion insert, e.g. `additionalTextEdits`.
141 *
142 * `additionalTextEdits` are An optional array of additional text edits that are applied when selecting this
143 * completion. Edits must not overlap (including the same insert position) with the main edit nor with themselves.
144 *
145 * Additional text edits should be used to change text unrelated to the current cursor position (for example adding an
146 * import statement at the top of the file if the completion item will insert an unqualified type).
147 */
148 static applyAdditionalTextEdits(event: ac.SuggestionInsertedEvent): void;
149 /**
150 * Public: Adds a snippet to the suggestion if the CompletionItem contains snippet-formatted text
151 *
152 * @param item An {CompletionItem} containing the completion items to be merged into.
153 * @param suggestion The {atom$AutocompleteSuggestion} to merge the conversion into.
154 */
155 static applySnippetToSuggestion(item: CompletionItem, suggestion: SnippetSuggestion): void;
156 /**
157 * Public: Obtain the textual suggestion type required by AutoComplete+ that most closely maps to the numeric
158 * completion kind supplies by the language server.
159 *
160 * @param kind A {Number} that represents the suggestion kind to be converted.
161 * @returns A {String} containing the AutoComplete+ suggestion type equivalent to the given completion kind.
162 */
163 static completionKindToSuggestionType(kind: number | undefined): string;
164}
165/**
166 * Normalizes the given grammar scope for autoComplete package so it always starts with `.` Based on
167 * https://github.com/atom/autocomplete-plus/wiki/Autocomplete-Providers
168 *
169 * @param grammarScope Such as 'source.python' or '.source.python'
170 * @returns The normalized grammarScope such as `.source.python`
171 */
172export declare function grammarScopeToAutoCompleteSelector(grammarScope: string): string;
173export {};