vscode#TextEditorEdit TypeScript Examples

The following examples show how to use vscode#TextEditorEdit. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: editor.ts    From dendron with GNU Affero General Public License v3.0 6 votes vote down vote up
/** Add a block anchor at the end of the specified line. The anchor is randomly generated if not supplied.
 *
 * If there is already an anchor at the end of this line, then this function doesn't actually insert an anchor but returns that anchor instead.
 *
 * @param editBuilder parameter of the callback in `editor.edit`
 * @param editor the editor that the editBuilder belongs to
 * @param position the line where the anchor will be inserted
 * @param anchor anchor id to insert (without ^), randomly generated if undefined
 * @returns the anchor that has been added (with ^)
 */
export function addOrGetAnchorAt(opts: {
  editBuilder: TextEditorEdit;
  editor: TextEditor;
  position: Position;
  anchor?: string;
  engine: DEngineClient;
}) {
  const { editBuilder, editor, position } = opts;
  let { anchor } = opts;
  const line = editor.document.lineAt(position.line);
  const existingAnchor = getAnchorAt(opts);
  if (!_.isUndefined(existingAnchor)) return existingAnchor;
  if (_.isUndefined(anchor)) anchor = genUUIDInsecure();
  editBuilder.insert(line.range.end, ` ^${anchor}`);
  return `^${anchor}`;
}
Example #2
Source File: commands.ts    From vscode-todo-md with MIT License 6 votes vote down vote up
/**
 * Sort tasks in editor. Default sort is by due date. Same due date sorted by priority.
 */
export function sortTasksInEditor(editor: TextEditor, edit: TextEditorEdit, sortProperty: SortProperty) {
	const selection = editor.selection;
	let lineStart = selection.start.line;
	let lineEnd = selection.end.line;
	if (selection.isEmpty) {
		lineStart = 0;
		lineEnd = editor.document.lineCount - 1;
	}
	const tasks: TheTask[] = [];
	for (let i = lineStart; i <= lineEnd; i++) {
		const task = getTaskAtLineExtension(i);
		if (task) {
			tasks.push(task);
		}
	}
	const sortedTasks = sortTasks(tasks, sortProperty);
	if (!sortedTasks.length) {
		return;
	}
	const result = sortedTasks.map(t => t.rawText).join('\n');
	edit.replace(getFullRangeFromLines(editor.document, lineStart, lineEnd), result);
}
Example #3
Source File: client.ts    From vala-vscode with MIT License 6 votes vote down vote up
peekSymbol(_editor: TextEditor, _edit: TextEditorEdit, lspCurrentLocation: lsp.Location, lspTargetLocation: lsp.Location): void {
        let currentLocation = new Location(
            Uri.parse(lspCurrentLocation.uri),
            new Range(
                new Position(lspCurrentLocation.range.start.line, lspCurrentLocation.range.start.character),
                new Position(lspCurrentLocation.range.end.line, lspCurrentLocation.range.end.character)
            )
        );
        let targetLocation = new Location(
            Uri.parse(lspTargetLocation.uri),
            new Range(
                new Position(lspTargetLocation.range.start.line, lspTargetLocation.range.start.character),
                new Position(lspTargetLocation.range.end.line, lspTargetLocation.range.end.character)
            )
        );

        commands.executeCommand(
            'editor.action.peekLocations',
            currentLocation.uri, // anchor uri and position
            currentLocation.range.end,
            [targetLocation], // results (vscode.Location[])
            'peek', // mode ('peek' | 'gotoAndPeek' | 'goto')
            'Nothing found' // <- message
        );
    }
Example #4
Source File: extension.ts    From vscode-plugin-swimming with MIT License 6 votes vote down vote up
function closeWriteCode(
    _textEditor: TextEditor,
    _edit: TextEditorEdit,
    ..._args: any[]
) {
    isWriteCodePauseMap.clear();
    isWritingCodeMap.clear();
    commands.executeCommand('workbench.action.reloadWindow');
}
Example #5
Source File: extension.ts    From vscode-plugin-swimming with MIT License 6 votes vote down vote up
function pauseWriteCode(
    textEditor: TextEditor,
    _edit: TextEditorEdit,
    ..._args: any[]
) {
    if(!isWritingCodeMap.get(textEditor.document.fileName)) {
        return window.showInformationMessage('rewriteCode not run,cannot pause.')
    }
    isWriteCodePauseMap.set(textEditor.document.fileName,!isWriteCodePauseMap.get(textEditor.document.fileName))
    showPauseinfo(textEditor);
}
Example #6
Source File: extension.ts    From format-imports-vscode with MIT License 5 votes vote down vote up
async function sortImportsByCommand(editor: TextEditor, _: TextEditorEdit, from?: TriggeredFrom) {
  if (!editor) return;
  const { document } = editor;
  if (!document) return;
  const newSourceText = await formatDocument(document, from === 'codeAction' ? from : 'onCommand');
  if (newSourceText === undefined) return;
  void editor.edit(edit => edit.replace(fullRange(document), newSourceText));
}
Example #7
Source File: extension.ts    From vscode-plugin-swimming with MIT License 5 votes vote down vote up
function rewriteCode(
    textEditor: TextEditor,
    edit: TextEditorEdit,
    _args: any[]
) {
    if(isWritingCodeMap.get(textEditor.document.fileName)) {
        return window.showInformationMessage('rewriteCode already in progress')
    }
    isWritingCodeMap.set(textEditor.document.fileName,true)
    isWriteCodePauseMap.set(textEditor.document.fileName,false)
    const { start, end } = textEditor.selection;
    let i = 0;
    let selectionRange = new Range(start, end);

    if (selectionRange.isEmpty) {
        const end = new Position(textEditor.document.lineCount + 1, 0);
        selectionRange = new Range(new Position(0, 0), end);
    }

    const beforeText = textEditor.document.getText(selectionRange);

    edit.delete(selectionRange);
    let { line, character } = textEditor.selection.start;
    const recycleWrite = function(inputTimeout: NodeJS.Timeout) {
        isWritingCodeMap.set(textEditor.document.fileName,false)
        clearTimeout(inputTimeout);
    }
    const runWrite = function() {
        const inputTimeout: NodeJS.Timeout = setTimeout(() => {
            if(isWriteCodePauseMap.get(textEditor.document.fileName)) {
                return textEditor.edit((_editBuilder) => {})
                .then((_value:boolean)=>{
                    return runWrite();
                },(reason)=>{
                    recycleWrite(inputTimeout)
                    throw new Error(reason);
                })
            }
            if (i >= beforeText.length || textEditor.document.isClosed) {
                return recycleWrite(inputTimeout)
            }
            const nowPosition = new Position(line, character);
            textEditor.edit((editBuilder) => {
                textEditor.revealRange(
                    new Range(nowPosition, nowPosition),
                    TextEditorRevealType.InCenter
                )
                if (beforeText.startsWith('\r\n', i)) {
                    character = 0;
                    line++
                    editBuilder.insert(nowPosition, '\r\n');
                    return i+=2;
                }
                if (beforeText.startsWith('\n', i)) {
                    character = 0;
                    line++
                    editBuilder.insert(nowPosition, '\n');
                    return i+=1;
                }
                editBuilder.insert(nowPosition, beforeText[i++]);
                character++;
            }).then((_value:boolean)=>{
                return runWrite();
            },(reason)=>{
                recycleWrite(inputTimeout)
                throw new Error(reason);
            })
        }, getReWriteSpeed());
    }
    runWrite();
    
}