VSCode hyperlink between results in one file to different file:line

扶醉桌前 提交于 2021-01-29 06:19:51

问题


I have some search results stored in a file, say results.foo. These results provide the file name and line number of each matching result. They look something like this:

bar1.c@123, bar2.c@678,
bar2.c@2345, bar3.c@444

What I'd like to do is, open results.foo in VSCode, have it scan the file (based on the extension), and "know" that clicking on the "123" in results.foo should open file bar1.c to line 123.

problemMatcher gets very close, but this seems action oriented (must invoke an external tool?) and would probably rely on the output window inside VS Code. I have created/used that type of operation inside Visual Studio IDE, but was hoping for simpler, file-to-file linking... Thanks!


回答1:


You can use the extension HTML Related Links v0.7.0. It does more than only HTML files.

To create a view with the links you can add the following setting (Global or Workspace)

  "html-related-links.include": {
    "all": [
      { "find": "([-\\w.]+)@(\\d+)", "lineNr": "$2" },
      { "find": "([-\\w.]+):(\\d+):(\\d+)", "lineNr": "$2", "charPos": "$3" }
    ]
  }

I also added a case if there is also a character position avaiable.

You have to set html-related-links.alwaysShow to true.

With a next release you can lock the content to a file.




回答2:


Here is another solution using a couple of extensions. One is a macro runner: multi-command to chain together the various commands. And the other is a way to parse and move within your filenames: Select By - thanks to @rioV8.

How this works: it doesn't create links out of your filenames, you put a cursor anywhere within one of the names and use a keybinding to trigger the macro.

Secondly, it uses the shell command code -g <someFileName>:<lineNumber> to go to that file and line number. Your files are of the form bar1.c@123 rather than bar1.c:@123. If they were in the later form the macro would be only two steps and so much shorter. As it is, it is required to parse bar1.c@123 into the filename and separately into the line number.

The macro (in settings.json):

  "multiCommand.commands": [

    {
      "command": "multiCommand.openFile",
      "sequence": [
        {
          "command": "moveby.regex",  // move to start of each filename
          "args": [
            "moveToFileNameStart",
            "moveby",
            "prev",
            "end"]
        },
        {
          "command": "selectby.regex",
          "args": ["fileNameSelect"]   // select just the filename part
        },
        {
          "command": "workbench.action.terminal.sendSequence",
          "args": {
            // send the filename: to the terminal
            "text": "code -g '${relativeFileDirname}\\${selectedText}':"
            // added ' ticks around the folder/filename if they should have spaces in them
          }
        },
        "cursorRight",
        "cursorRight",
        {
          "command": "selectby.regex",
          "args": ["gotoLineNumberSelect"]    // select just the linenumber
        },
        {
          "command": "workbench.action.terminal.sendSequence",
               // add the linenumber to the filename and add a return (\u000D)
          "args": {            
            "text": "${selectedText}\u000D"   // add the line number

            // add the line number and clear the terminal
            // "text": "${selectedText}; clear\u000D"
          }
        },
      ]
    },

Using the Select By extension (in settings.json):

  "selectby.regexes": {

    "moveToFileNameStart": {
      "flags": "m",

      //  "moveby": ",|^"    // works but I think below is better

      "moveby": ",(?!$)|^"   // works, move to start of line
                             //  or to "," that is not at the end of a line
    },

    "fileNameSelect": {
      "forward": " *",             // skip leading spaces, if any
      "forwardInclude": false,     
      "forwardNext": "@",          // get filename up to @ character
      "forwardNextInclude": false,
      "showSelection": true
    },

   "gotoLineNumberSelect": {
      "flags": "m",               // select the linenumber
      "forward": "(?=,)|$",       // select to "," or end of line
      "forwardInclude": true,
      "showSelection": true
    },
}

And a keybinding (keybindings.json) to start:

{
  "key": "alt+y",                  // whatever keybinding you wish
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.openFile" },
  // "when": "editorTextFocus && resourceFilename =~ /^results\.foo/"
},



Obviously, these are using relative filepaths. But sendSequence command will take the usual launch variables as well. See vscode: launch and task variables reference. Like ${relativeFileDirname} which you suggested in the comments.



来源:https://stackoverflow.com/questions/63139402/vscode-hyperlink-between-results-in-one-file-to-different-fileline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!