-
Notifications
You must be signed in to change notification settings - Fork 30.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commands): add
insertFinalNewLine
(#241126)
* feat(commands): add `insertFinalNewLine` * refactor: move `insertFinalNewLine` command from `common` folder * refactor: remove redunant code * very minor change
- Loading branch information
1 parent
a87baa4
commit df7858b
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/vs/editor/contrib/insertFinalNewLine/browser/insertFinalNewLine.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { ICodeEditor } from '../../../browser/editorBrowser.js'; | ||
import { EditorAction, registerEditorAction, ServicesAccessor } from '../../../browser/editorExtensions.js'; | ||
import { InsertFinalNewLineCommand } from './insertFinalNewLineCommand.js'; | ||
import { EditorContextKeys } from '../../../common/editorContextKeys.js'; | ||
import * as nls from '../../../../nls.js'; | ||
|
||
export class InsertFinalNewLineAction extends EditorAction { | ||
|
||
public static readonly ID = 'editor.action.insertFinalNewLine'; | ||
|
||
constructor() { | ||
super({ | ||
id: InsertFinalNewLineAction.ID, | ||
label: nls.localize2('insertFinalNewLine', "Insert Final New Line"), | ||
precondition: EditorContextKeys.writable | ||
}); | ||
} | ||
|
||
public run(_accessor: ServicesAccessor, editor: ICodeEditor, args: any): void { | ||
const selection = editor.getSelection(); | ||
if (selection === null) { | ||
return; | ||
} | ||
|
||
const command = new InsertFinalNewLineCommand(selection); | ||
|
||
editor.pushUndoStop(); | ||
editor.executeCommands(this.id, [command]); | ||
editor.pushUndoStop(); | ||
} | ||
} | ||
|
||
registerEditorAction(InsertFinalNewLineAction); |
54 changes: 54 additions & 0 deletions
54
src/vs/editor/contrib/insertFinalNewLine/browser/insertFinalNewLineCommand.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as strings from '../../../../base/common/strings.js'; | ||
import { EditOperation, ISingleEditOperation } from '../../../common/core/editOperation.js'; | ||
import { Position } from '../../../common/core/position.js'; | ||
import { Selection } from '../../../common/core/selection.js'; | ||
import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from '../../../common/editorCommon.js'; | ||
import { ITextModel } from '../../../common/model.js'; | ||
|
||
export class InsertFinalNewLineCommand implements ICommand { | ||
|
||
private readonly _selection: Selection; | ||
private _selectionId: string | null; | ||
|
||
|
||
constructor(selection: Selection) { | ||
this._selection = selection; | ||
this._selectionId = null; | ||
} | ||
|
||
public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void { | ||
const op = insertFinalNewLine(model); | ||
if (op) { | ||
builder.addEditOperation(op.range, op.text); | ||
} | ||
this._selectionId = builder.trackSelection(this._selection); | ||
} | ||
|
||
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection { | ||
return helper.getTrackedSelection(this._selectionId!); | ||
} | ||
} | ||
|
||
/** | ||
* Generate edit operations for inserting a final new line if needed. | ||
* Returns undefined if no edit is needed. | ||
*/ | ||
export function insertFinalNewLine(model: ITextModel): ISingleEditOperation | undefined { | ||
const lineCount = model.getLineCount(); | ||
const lastLine = model.getLineContent(lineCount); | ||
const lastLineIsEmptyOrWhitespace = strings.lastNonWhitespaceIndex(lastLine) === -1; | ||
|
||
if (!lineCount || lastLineIsEmptyOrWhitespace) { | ||
return; | ||
} | ||
|
||
return EditOperation.insert( | ||
new Position(lineCount, model.getLineMaxColumn(lineCount)), | ||
model.getEOL() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters