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