VSCode在一个文件到另一个文件的结果之间的超链接:行 [英] VSCode hyperlink between results in one file to different file:line

查看:57
本文介绍了VSCode在一个文件到另一个文件的结果之间的超链接:行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件中存储了一些搜索结果,例如results.foo.这些结果提供每个匹配结果的文件名和行号.他们看起来像这样:

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

我想做的是,在VSCode中打开results.foo,让它扫描文件(基于扩展名),然后知道"文件.点击"123"在results.foo中应打开文件bar1.c到第123行.

problemMatcher非常接近,但这似乎是面向操作的(必须调用外部工具吗?),并且可能依赖于VS Code中的输出窗口.我已经在Visual Studio IDE中创建/使用了这种类型的操作,但是希望进行更简单的文件到文件的链接...谢谢!

解决方案

这里是使用几个扩展名的另一个解决方案.一个是宏运行程序:


很明显,这些使用相对文件路径.但是 sendSequence 命令也将采用通常的启动变量.请参见 vscode:启动和任务变量参考.就像您在注释中建议的 $ {relativeFileDirname} .

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!

解决方案

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.

这篇关于VSCode在一个文件到另一个文件的结果之间的超链接:行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆