Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can VS Code code completion be configured to accept a suggestion on punctuation?

This question is particularly pointed at other C# devs coming over to TypeScript in VS Code.

I fell in love with the code completion in VS C#. To illustrate, say I'm trying to write:

console.log('hello')

Using C#, I would have:

  1. type "con"
  2. a list of suggestions would appear, probably starting with "console"
  3. since that's highlighted and it's what I want, hitting "." will write out "sole." so now I have: console.
  4. type "l", "log" is the first suggestion
  5. type "(", now I have: console.log('')
  6. cursor is now in the ''
  7. type "hello"

Currently with my VS Code setup, the same thing can be achieved in JS/TS hitting tab each time I want to accept a suggestion. But just hitting the next punctuation to proceed was really nice and, if you forgive me for caring about it, "fun." I miss it. And there's no technical limitation of the languages that I know of that would prohibit this behavior.

Anyone know if there's any extension or setting available to enable this? Or else, where else this conversation may be occurring ?

like image 323
John VanZwieten Avatar asked Oct 17 '22 05:10

John VanZwieten


1 Answers

One can implement this oneself using the macros extension. To do this:

  1. Install the macros extension

  2. Create a macro calling the acceptSelectedSuggestion action, then type .. Here's what my Settings.json looked like:

    {
        "editor.wordWrap": "on",
        "window.zoomLevel": 0,
        "git.enableSmartCommit": true,
        "macros": {
            "accept.": [
                "acceptSelectedSuggestion",
                {"command": "type", "args": {"text": "."}}
            ],
            "accept(": [
                "acceptSelectedSuggestion",
                {"command": "type", "args": {"text": "("}}
            ],        
            "accept=": [
                "acceptSelectedSuggestion",
                {"command": "type", "args": {"text": "="}}
            ]
        }
    }
    
  3. Added each of these macros to a key binding in keybindings.json. My additional keybindings looked like:

    {
        "key": ".",
        "command": "macros.accept.",
        "when": "editorTextFocus && suggestWidgetVisible"
    },
    {
        "key": "shift+9",
        "command": "macros.accept(",
        "when": "editorTextFocus && suggestWidgetVisible"
    },
    {
        "key": "=",
        "command": "macros.accept=",
        "when": "editorTextFocus && suggestWidgetVisible"
    }
    

    This enables the classic VS C# completion behavior for these 3 specific follow-on keys. Any others I will think of can be added as I remember them.

like image 124
John VanZwieten Avatar answered Oct 21 '22 01:10

John VanZwieten