Skip to content

Commit 3cc305c

Browse files
committed
Add support for commit characters
FEATURE: Completions may now provide 'commit characters' that, when typed, commit the completion before inserting the character. Issue codemirror/dev#1346
1 parent fb1c899 commit 3cc305c

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/completion.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export interface Completion {
3939
///
4040
/// Multiple types can be provided by separating them with spaces.
4141
type?: string
42+
/// When this option is selected, and one of these characters is
43+
/// typed, insert the completion before typing the character.
44+
commitCharacters?: readonly string[],
4245
/// When given, should be a number from -99 to 99 that adjusts how
4346
/// this completion is ranked compared to other completions that
4447
/// match the input as well as this one. A negative number moves it
@@ -229,6 +232,10 @@ export interface CompletionResult {
229232
/// [`validFor`](#autocomplete.CompletionResult.validFor)) that the
230233
/// completion still applies in the new state.
231234
update?: (current: CompletionResult, from: number, to: number, context: CompletionContext) => CompletionResult | null
235+
/// Set a default set of [commit
236+
/// characters](#autocomplete.Completion.commitCharacters) for all
237+
/// options in this result.
238+
commitCharacters?: readonly string[]
232239
}
233240

234241
export class Option {

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {keymap, KeyBinding} from "@codemirror/view"
33
import {Completion, Option} from "./completion"
44
import {completionState, State, setSelectedEffect} from "./state"
55
import {CompletionConfig, completionConfig} from "./config"
6-
import {completionPlugin, moveCompletionSelection, acceptCompletion, startCompletion, closeCompletion} from "./view"
6+
import {completionPlugin, moveCompletionSelection, acceptCompletion,
7+
startCompletion, closeCompletion, commitCharacters} from "./view"
78
import {baseTheme} from "./theme"
89

910
export {snippet, snippetCompletion, nextSnippetField, prevSnippetField,
@@ -17,6 +18,7 @@ export {CloseBracketConfig, closeBrackets, closeBracketsKeymap, deleteBracketPai
1718
/// Returns an extension that enables autocompletion.
1819
export function autocompletion(config: CompletionConfig = {}): Extension {
1920
return [
21+
commitCharacters,
2022
completionState,
2123
completionConfig.of(config),
2224
completionPlugin,

src/view.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {EditorView, Command, ViewPlugin, PluginValue, ViewUpdate, logException,
22
getTooltip, TooltipView} from "@codemirror/view"
3-
import {Transaction} from "@codemirror/state"
3+
import {Transaction, Prec} from "@codemirror/state"
44
import {completionState, setSelectedEffect, setActiveEffect, State,
55
ActiveSource, ActiveResult, getUserEvent, applyCompletion} from "./state"
66
import {completionConfig} from "./config"
@@ -216,3 +216,20 @@ export const completionPlugin = ViewPlugin.fromClass(class implements PluginValu
216216
}
217217
}
218218
})
219+
220+
const windows = typeof navigator == "object" && /Win/.test(navigator.platform)
221+
222+
export const commitCharacters = Prec.highest(EditorView.domEventHandlers({
223+
keydown(event, view) {
224+
let field = view.state.field(completionState, false)
225+
if (!field || !field.open || field.open.disabled || field.open.selected < 0 ||
226+
event.key.length > 1 || event.ctrlKey && !(windows && event.altKey) || event.metaKey)
227+
return false
228+
let option = field.open.options[field.open.selected]
229+
let result = field.active.find(a => a.source == option.source) as ActiveResult
230+
let commitChars = option.completion.commitCharacters || result.result.commitCharacters
231+
if (commitChars && commitChars.indexOf(event.key) > -1)
232+
applyCompletion(view, option)
233+
return false
234+
}
235+
}))

0 commit comments

Comments
 (0)