In a VSCode extension (Using Typescript) - How can I get the text content of all matches when the user executes a search in the Find Widget?

21 views Asked by At

I'm trying to implement a command in my VSCode extension that auto-trims the document, leaving nothing left but the found text from the find widget (joined by newlines).

Example image:

enter image description here

Ideally when I run my command, the only text remaining in the document are the woff2 urls (from the above example).

Can I directly get the contents of all matches as an array? Or do I need to execute editor.action.selectHighlights (Select All Occurrences of Find Match) first, and then get the content that way?

I've tried executing await vscode.commands.executeCommand('editor.action.selectHighlights');, but mysteriously this only seems to work when the find widget is in regex mode.

1

There are 1 answers

0
Mark On

This sequence of commands seems to work (at least as a keybinding):

{
  "key": "alt+c",
  "command": "runCommands",
  "args": {
    "commands": [
      "cancelSelection",     // necessary here because of seed search setting
      {
        "command": "editor.actions.findWithArgs",
        "args": {
          "searchString": "yourSearch",
          "isRegex": true
        }
      },
      "editor.action.selectAllMatches",       // try using this command
      "editor.action.clipboardCopyAction",
      "editor.action.selectAll",
      "deleteAllLeft",
      "editor.action.clipboardPasteAction"
    ]
  },
}

so try using editor.action.selectAllMatches instead of editor.action.selectHighlights in your extension.