VS代码功能可转到下一个空间

时间:2019-07-11 23:42:40

标签: javascript visual-studio-code vscode-extensions

我正在尝试编写一个函数,该函数将从游标移到下一个空格。这是我到目前为止的内容,但是我无法弄清楚如何从当前光标位置进行搜索:

// go to next space
function goToNextSpace(args) {
   const vscode = args.require('vscode');
   const regEx = /\s/;
   const ACTIVE_EDITOR = vscode.window.activeTextEditor;
   const text = ACTIVE_EDITOR.document.getText();
   const match = regEx.exec(text);
   const nextPos = ACTIVE_EDITOR.document.positionAt(match.index);

   return ACTIVE_EDITOR.selection = new vscode.Selection(
      nextPos, nextPos
   );
}

我一直在查看https://code.visualstudio.com/api/references/vscode-api,但是我根本找不到它引用了如何使用正则表达式的地方。

V2来自Matt的反馈

由于@MattBierner,我现在有了以下代码。我在索引上加了1,以确保每次调用它时,它都转到下一个空间(而不是仅停留在最后找到的空间上)。现在,我错过了一种方法,即使在一行的末尾也无法在下一行上移动下一个空格。

// go to next space
function goToNextSpace(args) {
   const vscode = args.require('vscode');
   const ACTIVE_EDITOR = vscode.window.activeTextEditor;
   const text = ACTIVE_EDITOR.document.getText();

   const regEx = /\s/g;
   regEx.lastIndex = ACTIVE_EDITOR.document.offsetAt(ACTIVE_EDITOR.selection.active) + 1;
   const match = regEx.exec(text);

   const nextPos = ACTIVE_EDITOR.document.positionAt(match.index);

   return ACTIVE_EDITOR.selection = new vscode.Selection(
      nextPos, nextPos
   );
}

2 个答案:

答案 0 :(得分:1)

尝试将正则表达式上的lastIndex属性设置为当前光标位置(ACTIVE_EDITOR.selection.active):

 // You also need to enable global matching mode on your regular expression
 const regEx = /\s/g;

 regEx.lastIndex = ACTIVE_EDITOR.document.offsetAt(ACTIVE_EDITOR.selection.active)

这应该确保您仅在光标位置之后找到匹配项

答案 1 :(得分:0)

将此用作vscode-powertools的命令。

  1. 安装vscode-powertools

  2. 在以下设置中定义powertools命令:%APPDATA%\Code\User\settings.json

    "ego.power-tools": {
        "commands": {
            "goToNextWhiteSpaceInDocument": {
                // Cannot use environment variables.. yet. https://github.com/microsoft/vscode/issues/2809
                // "script": "${env:APP_DATA}/goToNextWhiteSpaceInDocument.js",
                "script": "[absolute path to APPDATA with forward slashes]/Code/User/goToNextWhiteSpaceInDocument.js",
            },
            "goToPreviousWhiteSpaceInDocument": {
                // Cannot use environment variables.. yet. https://github.com/microsoft/vscode/issues/2809
                // "script": "${env:APP_DATA}/goToPreviousWhiteSpaceInDocument.js",
                "script": "[absolute path to APPDATA with forward slashes]/Code/User/goToPreviousWhiteSpaceInDocument.js",
            }
        }
    }
    
  3. 复制命令文件。

    1. command to go to previous white space复制到%APPDATA%\Code\User\goToPreviousWhiteSpaceInDocument.js
    2. command to go to next white space复制到%APPDATA%\Code\User\goToNextWhiteSpaceInDocument.js
  4. %APPDATA%\Code\User\keybindings.json中定义键盘快捷键

    {
        "key": "shift+alt+]",
        "command": "goToNextWhiteSpaceInDocument"
    },
    {
        "key": "shift+alt+[",
        "command": "goToPreviousWhiteSpaceInDocument"
    }
    

来自goToNextWhiteSpaceInDocument.js的代码如下。

exports.execute = async (args) => {
    // args => https://egodigital.github.io/vscode-powertools/api/interfaces/_contracts_.workspacecommandscriptarguments.html

    // https://code.visualstudio.com/api/references/vscode-api
    const vscode = args.require('vscode');

    // Current editor.
    const activeEditor = vscode.window.activeTextEditor;
    // Position of cursor - starting point of current selection.
    const currentPosition = activeEditor.selection.start;
    // Current document.
    const currentDocument = activeEditor.document;
    // Current line.
    const currentLineLength = currentDocument.lineAt(currentPosition.line).text.length;
    // Current offset within editor (from start of current selection, 
    // which will be where the cursor is).
    const currentOffset = currentDocument.offsetAt(currentPosition);

    /*
        Return true if current position is at end of the current line.
    */
    function atEndOfCurrentLine() {
        return currentPosition.character === currentLineLength;
    }

    /*
        Return true if current position is at end of file.
    */
    function atEndOfFile() {
        return atEndOfCurrentLine() && onLastLine();
    }

    /*
        Change position to end of current line.
    */
    function goToEndOfCurrentLine() {
        // Go to end of line.
        const nextPosition = currentDocument.lineAt(currentPosition.line).range.end;
        activeEditor.selection = new vscode.Selection(nextPosition /* start */, nextPosition /* end */);
    }

    /*
        Change position to start of next line.
    */
    function goToStartOfNextLine() {
        // Go to start of next line.
        const nextPosition = currentDocument.lineAt(currentPosition.line + 1).range.start;
        activeEditor.selection = new vscode.Selection(nextPosition /* start */, nextPosition /* end */);
    }

    /*
        Jump next word: jump to end of next set of white space after current set of non-white space.
    */
    function jumpNextWord() {
        // From current position to end of line.
        const textToEndOfLine = currentDocument.getText(
            new vscode.Range(
                currentPosition.line   /* start line */, currentPosition.character  /* start column */,
                currentPosition.line   /* end line   */, currentLineLength          /* end column   */,
            )
        );

        // Result of looking for first white space in rest of line.
        const regexResult = /\s+/.exec(textToEndOfLine);

        // No result - near end of line (but not at end - we already tested for that).
        if (!regexResult) {
            // Go to end of line.
            goToEndOfCurrentLine();
            // Do nothing more.
            return;
        } else {
            // Go forward to next space. Work out next position we want to be at.
            const nextPosition = activeEditor.document.positionAt(currentOffset + regexResult.index + regexResult[0].length);
            activeEditor.selection = new vscode.Selection(nextPosition /* start */, nextPosition /* end */);
            return;
        }
    }

    /*
        Return true if current position is on the last line of the file.
    */
    function onLastLine() {
        return currentPosition.line === currentDocument.lineCount - 1;
    }

    if(atEndOfFile()) {
        // Do nothing.
        vscode.window.showInformationMessage('At EOF.');
        return;
    } else if (atEndOfCurrentLine()) {
        goToStartOfNextLine();
    } else {
        jumpNextWord();
    }

};