Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/common/languages/enterAction.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Range } from '../core/range.js';
7
import { ITextModel } from '../model.js';
8
import { IndentAction, CompleteEnterAction } from './languageConfiguration.js';
9
import { EditorAutoIndentStrategy } from '../config/editorOptions.js';
10
import { getIndentationAtPosition, ILanguageConfigurationService } from './languageConfigurationRegistry.js';
11
import { IndentationContextProcessor } from './supports/indentationLineProcessor.js';
12
13
export function getEnterAction(
14
autoIndent: EditorAutoIndentStrategy,
15
model: ITextModel,
16
range: Range,
17
languageConfigurationService: ILanguageConfigurationService
18
): CompleteEnterAction | null {
19
model.tokenization.forceTokenization(range.startLineNumber);
20
const languageId = model.getLanguageIdAtPosition(range.startLineNumber, range.startColumn);
21
const richEditSupport = languageConfigurationService.getLanguageConfiguration(languageId);
22
if (!richEditSupport) {
23
return null;
24
}
25
const indentationContextProcessor = new IndentationContextProcessor(model, languageConfigurationService);
26
const processedContextTokens = indentationContextProcessor.getProcessedTokenContextAroundRange(range);
27
const previousLineText = processedContextTokens.previousLineProcessedTokens.getLineContent();
28
const beforeEnterText = processedContextTokens.beforeRangeProcessedTokens.getLineContent();
29
const afterEnterText = processedContextTokens.afterRangeProcessedTokens.getLineContent();
30
31
const enterResult = richEditSupport.onEnter(autoIndent, previousLineText, beforeEnterText, afterEnterText);
32
if (!enterResult) {
33
return null;
34
}
35
36
const indentAction = enterResult.indentAction;
37
let appendText = enterResult.appendText;
38
const removeText = enterResult.removeText || 0;
39
40
// Here we add `\t` to appendText first because enterAction is leveraging appendText and removeText to change indentation.
41
if (!appendText) {
42
if (
43
(indentAction === IndentAction.Indent) ||
44
(indentAction === IndentAction.IndentOutdent)
45
) {
46
appendText = '\t';
47
} else {
48
appendText = '';
49
}
50
} else if (indentAction === IndentAction.Indent) {
51
appendText = '\t' + appendText;
52
}
53
54
let indentation = getIndentationAtPosition(model, range.startLineNumber, range.startColumn);
55
if (removeText) {
56
indentation = indentation.substring(0, indentation.length - removeText);
57
}
58
59
return {
60
indentAction: indentAction,
61
appendText: appendText,
62
removeText: removeText,
63
indentation: indentation
64
};
65
}
66
67