Skip to content

Commit

Permalink
Prevent snippets when inside of a string
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenFlood committed Jul 15, 2021
1 parent c06a282 commit 0708946
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,4 @@ Type part of a snippet, press `tab` or `enter` to autofill the rest.

### Contributions

If you want any additional configurations or features feel free to either open an issue or pull request
If you want any additional configurations or features feel free to either [open an issue](https://github.com/OwenFlood/arduino-snippets/issues) or [pull request](https://github.com/OwenFlood/arduino-snippets/pulls)
7 changes: 6 additions & 1 deletion out/arduino-snippets.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion out/arduino-snippets.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions snippets/arduino-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ export function activate(context: vscode.ExtensionContext) {
provideCompletionItems(doc, pos, token, context) {
let sameLineCurly = vscode.workspace.getConfiguration("arduino-snippets-plus").get("same-line-curlies");

const string = doc.getText( // Contents of file up to current location
new vscode.Range(
new vscode.Position(0, 0),
new vscode.Position(pos.line, pos.character)
)
);
// | Number of quotations | - | Escaped quotations |
const isInQuote = !!((string.split('"').length - string.split('\\"').length) % 2);

if (isInQuote) { // Prevent returning snippets if cursor is in a string
return [];
}

return snippets.map(({ prefix, body, description }) => {
if (sameLineCurly && ~body.indexOf("\n{")) { // Searches body for presence of newline before curly
body = body.replace(/\n{/g, ' {');
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"rootDir": "snippets",
"resolveJsonModule": true,
"esModuleInterop": true,
"removeComments": true,
"strict": true /* enable all strict type-checking options */
/* Additional Checks */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
Expand Down

0 comments on commit 0708946

Please sign in to comment.