Path: blob/main/src/vs/editor/contrib/indentation/browser/indentation.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 { DisposableStore } from '../../../../base/common/lifecycle.js';6import * as strings from '../../../../base/common/strings.js';7import { ICodeEditor } from '../../../browser/editorBrowser.js';8import { EditorAction, EditorContributionInstantiation, IActionOptions, registerEditorAction, registerEditorContribution, ServicesAccessor } from '../../../browser/editorExtensions.js';9import { ShiftCommand } from '../../../common/commands/shiftCommand.js';10import { EditorAutoIndentStrategy, EditorOption } from '../../../common/config/editorOptions.js';11import { ISingleEditOperation } from '../../../common/core/editOperation.js';12import { IRange, Range } from '../../../common/core/range.js';13import { Selection } from '../../../common/core/selection.js';14import { ICommand, ICursorStateComputerData, IEditOperationBuilder, IEditorContribution } from '../../../common/editorCommon.js';15import { EditorContextKeys } from '../../../common/editorContextKeys.js';16import { EndOfLineSequence, ITextModel } from '../../../common/model.js';17import { TextEdit } from '../../../common/languages.js';18import { StandardTokenType } from '../../../common/encodedTokenAttributes.js';19import { ILanguageConfigurationService } from '../../../common/languages/languageConfigurationRegistry.js';20import { IndentConsts } from '../../../common/languages/supports/indentRules.js';21import { IModelService } from '../../../common/services/model.js';22import * as indentUtils from '../common/indentUtils.js';23import * as nls from '../../../../nls.js';24import { IQuickInputService } from '../../../../platform/quickinput/common/quickInput.js';25import { getGoodIndentForLine, getIndentMetadata } from '../../../common/languages/autoIndent.js';26import { getReindentEditOperations } from '../common/indentation.js';27import { getStandardTokenTypeAtPosition } from '../../../common/tokens/lineTokens.js';28import { Position } from '../../../common/core/position.js';2930export class IndentationToSpacesAction extends EditorAction {31public static readonly ID = 'editor.action.indentationToSpaces';3233constructor() {34super({35id: IndentationToSpacesAction.ID,36label: nls.localize2('indentationToSpaces', "Convert Indentation to Spaces"),37precondition: EditorContextKeys.writable,38metadata: {39description: nls.localize2('indentationToSpacesDescription', "Convert the tab indentation to spaces."),40}41});42}4344public run(accessor: ServicesAccessor, editor: ICodeEditor): void {45const model = editor.getModel();46if (!model) {47return;48}49const modelOpts = model.getOptions();50const selection = editor.getSelection();51if (!selection) {52return;53}54const command = new IndentationToSpacesCommand(selection, modelOpts.tabSize);5556editor.pushUndoStop();57editor.executeCommands(this.id, [command]);58editor.pushUndoStop();5960model.updateOptions({61insertSpaces: true62});63}64}6566export class IndentationToTabsAction extends EditorAction {67public static readonly ID = 'editor.action.indentationToTabs';6869constructor() {70super({71id: IndentationToTabsAction.ID,72label: nls.localize2('indentationToTabs', "Convert Indentation to Tabs"),73precondition: EditorContextKeys.writable,74metadata: {75description: nls.localize2('indentationToTabsDescription', "Convert the spaces indentation to tabs."),76}77});78}7980public run(accessor: ServicesAccessor, editor: ICodeEditor): void {81const model = editor.getModel();82if (!model) {83return;84}85const modelOpts = model.getOptions();86const selection = editor.getSelection();87if (!selection) {88return;89}90const command = new IndentationToTabsCommand(selection, modelOpts.tabSize);9192editor.pushUndoStop();93editor.executeCommands(this.id, [command]);94editor.pushUndoStop();9596model.updateOptions({97insertSpaces: false98});99}100}101102export class ChangeIndentationSizeAction extends EditorAction {103104constructor(private readonly insertSpaces: boolean, private readonly displaySizeOnly: boolean, opts: IActionOptions) {105super(opts);106}107108public run(accessor: ServicesAccessor, editor: ICodeEditor): void {109const quickInputService = accessor.get(IQuickInputService);110const modelService = accessor.get(IModelService);111112const model = editor.getModel();113if (!model) {114return;115}116117const creationOpts = modelService.getCreationOptions(model.getLanguageId(), model.uri, model.isForSimpleWidget);118const modelOpts = model.getOptions();119const picks = [1, 2, 3, 4, 5, 6, 7, 8].map(n => ({120id: n.toString(),121label: n.toString(),122// add description for tabSize value set in the configuration123description: (124n === creationOpts.tabSize && n === modelOpts.tabSize125? nls.localize('configuredTabSize', "Configured Tab Size")126: n === creationOpts.tabSize127? nls.localize('defaultTabSize', "Default Tab Size")128: n === modelOpts.tabSize129? nls.localize('currentTabSize', "Current Tab Size")130: undefined131)132}));133134// auto focus the tabSize set for the current editor135const autoFocusIndex = Math.min(model.getOptions().tabSize - 1, 7);136137setTimeout(() => {138quickInputService.pick(picks, { placeHolder: nls.localize({ key: 'selectTabWidth', comment: ['Tab corresponds to the tab key'] }, "Select Tab Size for Current File"), activeItem: picks[autoFocusIndex] }).then(pick => {139if (pick) {140if (model && !model.isDisposed()) {141const pickedVal = parseInt(pick.label, 10);142if (this.displaySizeOnly) {143model.updateOptions({144tabSize: pickedVal145});146} else {147model.updateOptions({148tabSize: pickedVal,149indentSize: pickedVal,150insertSpaces: this.insertSpaces151});152}153}154}155});156}, 50/* quick input is sensitive to being opened so soon after another */);157}158}159160export class IndentUsingTabs extends ChangeIndentationSizeAction {161162public static readonly ID = 'editor.action.indentUsingTabs';163164constructor() {165super(false, false, {166id: IndentUsingTabs.ID,167label: nls.localize2('indentUsingTabs', "Indent Using Tabs"),168precondition: undefined,169metadata: {170description: nls.localize2('indentUsingTabsDescription', "Use indentation with tabs."),171}172});173}174}175176export class IndentUsingSpaces extends ChangeIndentationSizeAction {177178public static readonly ID = 'editor.action.indentUsingSpaces';179180constructor() {181super(true, false, {182id: IndentUsingSpaces.ID,183label: nls.localize2('indentUsingSpaces', "Indent Using Spaces"),184precondition: undefined,185metadata: {186description: nls.localize2('indentUsingSpacesDescription', "Use indentation with spaces."),187}188});189}190}191192export class ChangeTabDisplaySize extends ChangeIndentationSizeAction {193194public static readonly ID = 'editor.action.changeTabDisplaySize';195196constructor() {197super(true, true, {198id: ChangeTabDisplaySize.ID,199label: nls.localize2('changeTabDisplaySize', "Change Tab Display Size"),200precondition: undefined,201metadata: {202description: nls.localize2('changeTabDisplaySizeDescription', "Change the space size equivalent of the tab."),203}204});205}206}207208export class DetectIndentation extends EditorAction {209210public static readonly ID = 'editor.action.detectIndentation';211212constructor() {213super({214id: DetectIndentation.ID,215label: nls.localize2('detectIndentation', "Detect Indentation from Content"),216precondition: undefined,217metadata: {218description: nls.localize2('detectIndentationDescription', "Detect the indentation from content."),219}220});221}222223public run(accessor: ServicesAccessor, editor: ICodeEditor): void {224const modelService = accessor.get(IModelService);225226const model = editor.getModel();227if (!model) {228return;229}230231const creationOpts = modelService.getCreationOptions(model.getLanguageId(), model.uri, model.isForSimpleWidget);232model.detectIndentation(creationOpts.insertSpaces, creationOpts.tabSize);233}234}235236export class ReindentLinesAction extends EditorAction {237constructor() {238super({239id: 'editor.action.reindentlines',240label: nls.localize2('editor.reindentlines', "Reindent Lines"),241precondition: EditorContextKeys.writable,242metadata: {243description: nls.localize2('editor.reindentlinesDescription', "Reindent the lines of the editor."),244}245});246}247248public run(accessor: ServicesAccessor, editor: ICodeEditor): void {249const languageConfigurationService = accessor.get(ILanguageConfigurationService);250251const model = editor.getModel();252if (!model) {253return;254}255const edits = getReindentEditOperations(model, languageConfigurationService, 1, model.getLineCount());256if (edits.length > 0) {257editor.pushUndoStop();258editor.executeEdits(this.id, edits);259editor.pushUndoStop();260}261}262}263264export class ReindentSelectedLinesAction extends EditorAction {265constructor() {266super({267id: 'editor.action.reindentselectedlines',268label: nls.localize2('editor.reindentselectedlines', "Reindent Selected Lines"),269precondition: EditorContextKeys.writable,270metadata: {271description: nls.localize2('editor.reindentselectedlinesDescription', "Reindent the selected lines of the editor."),272}273});274}275276public run(accessor: ServicesAccessor, editor: ICodeEditor): void {277const languageConfigurationService = accessor.get(ILanguageConfigurationService);278279const model = editor.getModel();280if (!model) {281return;282}283284const selections = editor.getSelections();285if (selections === null) {286return;287}288289const edits: ISingleEditOperation[] = [];290291for (const selection of selections) {292let startLineNumber = selection.startLineNumber;293let endLineNumber = selection.endLineNumber;294295if (startLineNumber !== endLineNumber && selection.endColumn === 1) {296endLineNumber--;297}298299if (startLineNumber === 1) {300if (startLineNumber === endLineNumber) {301continue;302}303} else {304startLineNumber--;305}306307const editOperations = getReindentEditOperations(model, languageConfigurationService, startLineNumber, endLineNumber);308edits.push(...editOperations);309}310311if (edits.length > 0) {312editor.pushUndoStop();313editor.executeEdits(this.id, edits);314editor.pushUndoStop();315}316}317}318319export class AutoIndentOnPasteCommand implements ICommand {320321private readonly _edits: { range: IRange; text: string; eol?: EndOfLineSequence }[];322323private readonly _initialSelection: Selection;324private _selectionId: string | null;325326constructor(edits: TextEdit[], initialSelection: Selection) {327this._initialSelection = initialSelection;328this._edits = [];329this._selectionId = null;330331for (const edit of edits) {332if (edit.range && typeof edit.text === 'string') {333this._edits.push(edit as { range: IRange; text: string; eol?: EndOfLineSequence });334}335}336}337338public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {339for (const edit of this._edits) {340builder.addEditOperation(Range.lift(edit.range), edit.text);341}342343let selectionIsSet = false;344if (Array.isArray(this._edits) && this._edits.length === 1 && this._initialSelection.isEmpty()) {345if (this._edits[0].range.startColumn === this._initialSelection.endColumn &&346this._edits[0].range.startLineNumber === this._initialSelection.endLineNumber) {347selectionIsSet = true;348this._selectionId = builder.trackSelection(this._initialSelection, true);349} else if (this._edits[0].range.endColumn === this._initialSelection.startColumn &&350this._edits[0].range.endLineNumber === this._initialSelection.startLineNumber) {351selectionIsSet = true;352this._selectionId = builder.trackSelection(this._initialSelection, false);353}354}355356if (!selectionIsSet) {357this._selectionId = builder.trackSelection(this._initialSelection);358}359}360361public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {362return helper.getTrackedSelection(this._selectionId!);363}364}365366export class AutoIndentOnPaste implements IEditorContribution {367public static readonly ID = 'editor.contrib.autoIndentOnPaste';368369private readonly callOnDispose = new DisposableStore();370private readonly callOnModel = new DisposableStore();371372constructor(373private readonly editor: ICodeEditor,374@ILanguageConfigurationService private readonly _languageConfigurationService: ILanguageConfigurationService375) {376377this.callOnDispose.add(editor.onDidChangeConfiguration(() => this.update()));378this.callOnDispose.add(editor.onDidChangeModel(() => this.update()));379this.callOnDispose.add(editor.onDidChangeModelLanguage(() => this.update()));380}381382private update(): void {383384// clean up385this.callOnModel.clear();386387// we are disabled388if (!this.editor.getOption(EditorOption.autoIndentOnPaste) || this.editor.getOption(EditorOption.autoIndent) < EditorAutoIndentStrategy.Full) {389return;390}391392// no model393if (!this.editor.hasModel()) {394return;395}396397this.callOnModel.add(this.editor.onDidPaste(({ range }) => {398this.trigger(range);399}));400}401402public trigger(range: Range): void {403const selections = this.editor.getSelections();404if (selections === null || selections.length > 1) {405return;406}407408const model = this.editor.getModel();409if (!model) {410return;411}412const containsOnlyWhitespace = this.rangeContainsOnlyWhitespaceCharacters(model, range);413if (containsOnlyWhitespace) {414return;415}416if (!this.editor.getOption(EditorOption.autoIndentOnPasteWithinString) && isStartOrEndInString(model, range)) {417return;418}419if (!model.tokenization.isCheapToTokenize(range.getStartPosition().lineNumber)) {420return;421}422const autoIndent = this.editor.getOption(EditorOption.autoIndent);423const { tabSize, indentSize, insertSpaces } = model.getOptions();424const textEdits: TextEdit[] = [];425426const indentConverter = {427shiftIndent: (indentation: string) => {428return ShiftCommand.shiftIndent(indentation, indentation.length + 1, tabSize, indentSize, insertSpaces);429},430unshiftIndent: (indentation: string) => {431return ShiftCommand.unshiftIndent(indentation, indentation.length + 1, tabSize, indentSize, insertSpaces);432}433};434435let startLineNumber = range.startLineNumber;436437let firstLineText = model.getLineContent(startLineNumber);438if (!/\S/.test(firstLineText.substring(0, range.startColumn - 1))) {439const indentOfFirstLine = getGoodIndentForLine(autoIndent, model, model.getLanguageId(), startLineNumber, indentConverter, this._languageConfigurationService);440441if (indentOfFirstLine !== null) {442const oldIndentation = strings.getLeadingWhitespace(firstLineText);443const newSpaceCnt = indentUtils.getSpaceCnt(indentOfFirstLine, tabSize);444const oldSpaceCnt = indentUtils.getSpaceCnt(oldIndentation, tabSize);445446if (newSpaceCnt !== oldSpaceCnt) {447const newIndent = indentUtils.generateIndent(newSpaceCnt, tabSize, insertSpaces);448textEdits.push({449range: new Range(startLineNumber, 1, startLineNumber, oldIndentation.length + 1),450text: newIndent451});452firstLineText = newIndent + firstLineText.substring(oldIndentation.length);453} else {454const indentMetadata = getIndentMetadata(model, startLineNumber, this._languageConfigurationService);455456if (indentMetadata === 0 || indentMetadata === IndentConsts.UNINDENT_MASK) {457// we paste content into a line where only contains whitespaces458// after pasting, the indentation of the first line is already correct459// the first line doesn't match any indentation rule460// then no-op.461return;462}463}464}465}466467const firstLineNumber = startLineNumber;468469// ignore empty or ignored lines470while (startLineNumber < range.endLineNumber) {471if (!/\S/.test(model.getLineContent(startLineNumber + 1))) {472startLineNumber++;473continue;474}475break;476}477478if (startLineNumber !== range.endLineNumber) {479const virtualModel = {480tokenization: {481getLineTokens: (lineNumber: number) => {482return model.tokenization.getLineTokens(lineNumber);483},484getLanguageId: () => {485return model.getLanguageId();486},487getLanguageIdAtPosition: (lineNumber: number, column: number) => {488return model.getLanguageIdAtPosition(lineNumber, column);489},490},491getLineContent: (lineNumber: number) => {492if (lineNumber === firstLineNumber) {493return firstLineText;494} else {495return model.getLineContent(lineNumber);496}497}498};499const indentOfSecondLine = getGoodIndentForLine(autoIndent, virtualModel, model.getLanguageId(), startLineNumber + 1, indentConverter, this._languageConfigurationService);500if (indentOfSecondLine !== null) {501const newSpaceCntOfSecondLine = indentUtils.getSpaceCnt(indentOfSecondLine, tabSize);502const oldSpaceCntOfSecondLine = indentUtils.getSpaceCnt(strings.getLeadingWhitespace(model.getLineContent(startLineNumber + 1)), tabSize);503504if (newSpaceCntOfSecondLine !== oldSpaceCntOfSecondLine) {505const spaceCntOffset = newSpaceCntOfSecondLine - oldSpaceCntOfSecondLine;506for (let i = startLineNumber + 1; i <= range.endLineNumber; i++) {507const lineContent = model.getLineContent(i);508const originalIndent = strings.getLeadingWhitespace(lineContent);509const originalSpacesCnt = indentUtils.getSpaceCnt(originalIndent, tabSize);510const newSpacesCnt = originalSpacesCnt + spaceCntOffset;511const newIndent = indentUtils.generateIndent(newSpacesCnt, tabSize, insertSpaces);512513if (newIndent !== originalIndent) {514textEdits.push({515range: new Range(i, 1, i, originalIndent.length + 1),516text: newIndent517});518}519}520}521}522}523524if (textEdits.length > 0) {525this.editor.pushUndoStop();526const cmd = new AutoIndentOnPasteCommand(textEdits, this.editor.getSelection()!);527this.editor.executeCommand('autoIndentOnPaste', cmd);528this.editor.pushUndoStop();529}530}531532private rangeContainsOnlyWhitespaceCharacters(model: ITextModel, range: Range): boolean {533const lineContainsOnlyWhitespace = (content: string): boolean => {534return content.trim().length === 0;535};536let containsOnlyWhitespace: boolean = true;537if (range.startLineNumber === range.endLineNumber) {538const lineContent = model.getLineContent(range.startLineNumber);539const linePart = lineContent.substring(range.startColumn - 1, range.endColumn - 1);540containsOnlyWhitespace = lineContainsOnlyWhitespace(linePart);541} else {542for (let i = range.startLineNumber; i <= range.endLineNumber; i++) {543const lineContent = model.getLineContent(i);544if (i === range.startLineNumber) {545const linePart = lineContent.substring(range.startColumn - 1);546containsOnlyWhitespace = lineContainsOnlyWhitespace(linePart);547} else if (i === range.endLineNumber) {548const linePart = lineContent.substring(0, range.endColumn - 1);549containsOnlyWhitespace = lineContainsOnlyWhitespace(linePart);550} else {551containsOnlyWhitespace = model.getLineFirstNonWhitespaceColumn(i) === 0;552}553if (!containsOnlyWhitespace) {554break;555}556}557}558return containsOnlyWhitespace;559}560561public dispose(): void {562this.callOnDispose.dispose();563this.callOnModel.dispose();564}565}566567function isStartOrEndInString(model: ITextModel, range: Range): boolean {568const isPositionInString = (position: Position): boolean => {569const tokenType = getStandardTokenTypeAtPosition(model, position);570return tokenType === StandardTokenType.String;571};572return isPositionInString(range.getStartPosition()) || isPositionInString(range.getEndPosition());573}574575function getIndentationEditOperations(model: ITextModel, builder: IEditOperationBuilder, tabSize: number, tabsToSpaces: boolean): void {576if (model.getLineCount() === 1 && model.getLineMaxColumn(1) === 1) {577// Model is empty578return;579}580581let spaces = '';582for (let i = 0; i < tabSize; i++) {583spaces += ' ';584}585586const spacesRegExp = new RegExp(spaces, 'gi');587588for (let lineNumber = 1, lineCount = model.getLineCount(); lineNumber <= lineCount; lineNumber++) {589let lastIndentationColumn = model.getLineFirstNonWhitespaceColumn(lineNumber);590if (lastIndentationColumn === 0) {591lastIndentationColumn = model.getLineMaxColumn(lineNumber);592}593594if (lastIndentationColumn === 1) {595continue;596}597598const originalIndentationRange = new Range(lineNumber, 1, lineNumber, lastIndentationColumn);599const originalIndentation = model.getValueInRange(originalIndentationRange);600const newIndentation = (601tabsToSpaces602? originalIndentation.replace(/\t/ig, spaces)603: originalIndentation.replace(spacesRegExp, '\t')604);605606builder.addEditOperation(originalIndentationRange, newIndentation);607}608}609610export class IndentationToSpacesCommand implements ICommand {611612private selectionId: string | null = null;613614constructor(private readonly selection: Selection, private tabSize: number) { }615616public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {617this.selectionId = builder.trackSelection(this.selection);618getIndentationEditOperations(model, builder, this.tabSize, true);619}620621public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {622return helper.getTrackedSelection(this.selectionId!);623}624}625626export class IndentationToTabsCommand implements ICommand {627628private selectionId: string | null = null;629630constructor(private readonly selection: Selection, private tabSize: number) { }631632public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {633this.selectionId = builder.trackSelection(this.selection);634getIndentationEditOperations(model, builder, this.tabSize, false);635}636637public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {638return helper.getTrackedSelection(this.selectionId!);639}640}641642registerEditorContribution(AutoIndentOnPaste.ID, AutoIndentOnPaste, EditorContributionInstantiation.BeforeFirstInteraction);643registerEditorAction(IndentationToSpacesAction);644registerEditorAction(IndentationToTabsAction);645registerEditorAction(IndentUsingTabs);646registerEditorAction(IndentUsingSpaces);647registerEditorAction(ChangeTabDisplaySize);648registerEditorAction(DetectIndentation);649registerEditorAction(ReindentLinesAction);650registerEditorAction(ReindentSelectedLinesAction);651652653