ipatalas / vscode-postfix-ts

Postfix notation for TypeScript/Javascript - extension for VS Code
MIT License
158 stars 43 forks source link

Incorrect insertion when in mid of expression #60

Closed zardoy closed 2 years ago

zardoy commented 2 years ago

Hey! I have the following snippet:

{
    "name": "entries",
    "description": "",
    "body": "Object.entries({{expr}})",
    "when": ["identifier", "expression", "function-call"]
}

And I usually like to use it in the middle of expression to fix TS errors like this:

a.entries.a

However:

ipatalas commented 2 years ago

Oh, this might be tricky as the idea was to only analyze what's before the triggering postfix template. That means it was always meant to be used at the end of the expression so it might be tricky but I'll have a look.

Btw can you give me more meaningful example of why would you like to auto-complete in the middle? I don't get this example above.

zardoy commented 2 years ago

Yes! I remembered two snippets, where this behavior was really annoying. Consider these extremely useful snippets:

{
    "name": "moreArr",
    "description": "",
    "body": "[...{{expr}}, $1]",
    "when": ["identifier", "expression", "function-call"]
},
{
    "name": "moreObj",
    "description": "",
    "body": "{...{{expr}}, $1}",
    "when": ["identifier", "expression", "function-call"]
},

Example code:

const someStrings = ['I', 'love']

console.log(someStrings/*.moreArr*/.reduce((prev, str) => `${prev} ${str}`, ''))

// actual (see issue body for different cases)

console.log([...someStrings., ].reduce((prev, str) => `${prev} ${str}`, ''))

// expected

console.log([...someStrings, 'postfixes'].reduce((prev, str) => `${prev} ${str}`, ''))

// another example

Of course, I could wrap this expression into brackets and write ... but I would lose a lot of time on this (that's why I use this extension!).

ipatalas commented 2 years ago

Cool, that makes sense, I'll have a look.

ipatalas commented 2 years ago

Published a fix