Skip to content

Commit 44b79b2

Browse files
authored
Merge pull request #9 from myaaghubi/v1.5.4
V1.5.4
2 parents 92e5e57 + 12647a1 commit 44b79b2

File tree

4 files changed

+99
-54
lines changed

4 files changed

+99
-54
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Change Log
22

3-
## 1.5.3 (latest)
3+
## 1.5.4 (latest)
4+
5+
Some fixes & improvements
6+
7+
## 1.5.3 (9/4/2024)
48

59
- Some fixes with lines ending with a comment
610
- Support the languages with different comment delimiter/syntax

README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,51 @@
44
[![release](https://img.shields.io/github/release/myaaghubi/Auto-Semicolon-VSCode.svg?style=for-the-badge&logo=github&logoColor=white&colorA=2b303b&colorB=00e8c6)](https://github.com/myaaghubi/Auto-Semicolon-VSCode/releases/latest)
55
[![rating](https://img.shields.io/visual-studio-marketplace/stars/myaaghubi.auto-semicolon-vscode?style=for-the-badge&logo=reverbnation&logoColor=white&colorA=2b303b&colorB=FFE66D)](https://marketplace.visualstudio.com/items?itemName=myaaghubi.auto-semicolon-vscode)
66

7-
A handy tool to make programming delicious.
7+
A useful tool to enhance coding with VSCode.
88

9-
This extension helps by moving to the end of the line `and/or` putting `;` in the right place.
9+
This extension assists by moving the cursor to the end of the line `and/or` putting the `;` at the end of the line.
1010

11-
All programming languages include both `Semicolon separated` and `Non-Semicolon separated` supported.
12-
It's **`auto`**, **`smart`**, **`handy`**, and **`customizable`**. Don't forget to [🍔 donate](#-donate) if you find joy in it.
11+
All programming languages including both `Semicolon separated` and `Non-Semicolon separated` are supported.
12+
It's **`auto`**, **`smart`**, **`handy`**, and **`customizable`**.
1313

14-
I found nothing for `PHP` so made my own, now it's the `best` for this use.
14+
I found nothing for `PHP` so made my shit, now it's the `best` for all languages.
1515

16-
## How To Use
17-
1. Install it from [market place > auto semicolon](https://marketplace.visualstudio.com/items?itemName=myaaghubi.auto-semicolon-vscode).
18-
2. Use `;` don't need to move your mouse.
16+
You may interested in [🍔 donating](#-donate).
1917

18+
## Install/Use
19+
1. Install it from [market place > auto semicolon](https://marketplace.visualstudio.com/items?itemName=myaaghubi.auto-semicolon-vscode)
20+
2. Press `;` anywhere
21+
Enjoy it
22+
23+
## Notes
24+
- Customizable by settings.
25+
- `Multiple cursors` has been supported.
26+
- Excluding quotes `` "'` `` supported (check it in settings).
27+
- The `for(...;...;)` statement supported (check it in settings).
28+
- `PHP`, `javascript`, `typescript`, `c#`, `c/c++`, `java`, `perl`, `dart`, `swift`, **even** `python`, `go`, `bash`, `scala`, `kotlin`, `r` supported, feel free to add your programming language in the list!
2029

2130
## Shortcuts
2231
- `;` > To have it auto
23-
- `alt + ;` then `;` > To put `;` right in place (not auto)
32+
- `alt + ;` then `;` > To put `;` manual (not auto)
2433

25-
Or use the command palette `ctrl+shift+p` > `Auto Semicolon > Insert At The Position`.
34+
Or use the command palette `ctrl+shift+p` > `Insert Manually`.
2635
- `alt` + twice `;` > To ignore enclosing curly bracket pair `{..}` (force move to the end of the line)
2736

28-
Or use `ctrl+shift+p` > `Auto Semicolon > Auto Insert At The End`.
37+
Or use `ctrl+shift+p` > `Insert Force To The End`.
2938
You can change this option from settings.
3039

31-
## Notes
32-
- `Multiple cursors` has been supported.
33-
- `PHP`, `javascript`, `typescript`, `c#`, `c/c++`, `java`, `perl`, `dart`, `swift`, even `python`, `go`, `bash`, `scala`, `kotlin`, `r` supported, feel free to ask support for your programming language!
40+
## Keybinding
41+
You can customize key bindings, to do that
42+
- Open Command Palette (`ctrl+shift+p`)
43+
- Search for 'Open keyboard shortcuts' then 'Auto Semicolon` > `Change keybinding`
44+
```json
45+
This is the default:
46+
[
47+
{ "key": "alt+; ;", "command": "auto-semicolon-vscode.position-insert-semicolon", "when": "editorTextFocus" },
48+
{ "key": "alt+; ;", "command": "auto-semicolon-vscode.position-insert-semicolon", "when": "editorTextFocus" },
49+
{ "key": "alt+; alt+;", "command": "auto-semicolon-vscode.auto-insert-semicolon-fte", "when": "editorTextFocus" }
50+
]
51+
```
3452

3553
## Preview
3654
![Shot Auto-Semicolon-VSCode](assets/auto-semicolon1.gif)
@@ -40,7 +58,7 @@ I found nothing for `PHP` so made my own, now it's the `best` for this use.
4058
![Shot Auto-Semicolon-VSCode](assets/auto-semicolon3.gif)
4159

4260
## 🍔 Donate
43-
Don't forget to donate if you enjoyed it ☕ 🍺 🍸 🍔
61+
Pick one to donate ☕ 🍺 🍸 🍔
4462

4563
ETH: 0x0ADd51D6855d2DF11BB5F331A3fa345c67a863b2
4664

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "auto-semicolon-vscode",
33
"displayName": "Auto Semicolon",
44
"description": "A handy tool to make programming delicious. All programming languages include both `Semicolon separated` and `Non-Semicolon separated` supported.",
5-
"version": "1.5.3",
5+
"version": "1.5.4",
66
"publisher": "myaaghubi",
77
"license": "GPL-3.0",
88
"icon": "icon.png",
@@ -20,13 +20,12 @@
2020
"Other"
2121
],
2222
"keywords": [
23-
"auto-semicolon",
2423
"helper",
25-
"keybindings",
26-
"shortcut",
2724
"auto",
28-
"semicolon",
2925
"handy",
26+
"comment",
27+
"auto-semicolon",
28+
"semicolon",
3029
"php",
3130
"perl",
3231
"javascript",
@@ -46,7 +45,9 @@
4645
"bash",
4746
"bat",
4847
"scala",
49-
"kotlin"
48+
"kotlin",
49+
"keybindings",
50+
"shortcut"
5051
],
5152
"activationEvents": [
5253
"onStartupFinished",
@@ -59,7 +60,7 @@
5960
"commands": [
6061
{
6162
"command": "auto-semicolon-vscode.position-insert-semicolon",
62-
"title": "Insert At The Position",
63+
"title": "Insert Manually",
6364
"category": "Auto Semicolon"
6465
},
6566
{
@@ -69,7 +70,7 @@
6970
},
7071
{
7172
"command": "auto-semicolon-vscode.auto-insert-semicolon-fte",
72-
"title": "Auto Insert At The End",
73+
"title": "Insert Force To The End",
7374
"category": "Auto Semicolon"
7475
}
7576
],

src/extension.ts

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import * as vscode from 'vscode';
33

44
export async function activate(context: vscode.ExtensionContext) {
5+
devMode = context.extensionMode === vscode.ExtensionMode.Development;
6+
57
let semicolonAtPosition = vscode.commands.registerTextEditorCommand('auto-semicolon-vscode.position-insert-semicolon',
68
(editor: vscode.TextEditor, textEdit: vscode.TextEditorEdit) => {
79
return semicolonCommand(editor, textEdit);
@@ -33,16 +35,18 @@ export function deactivate() { }
3335
type RegExpMatchArrayWithIndex = RegExpMatchArray & { index: number };
3436

3537
// these variables will fill later
38+
let devMode = false;
3639
let autoSemicolonFormatsIncluded = true;
3740
let autoMoveFormatsIncluded = false;
3841
let commentDelimiter = '//';
42+
let commentDelimiterIndex = -1;
3943

4044

4145
type StringDictionary = {
42-
[key: string]: string;
46+
[key: string]: string;
4347
};
4448

45-
const languagesDelimiter:StringDictionary = {
49+
const languagesDelimiter: StringDictionary = {
4650
"dotenv": '#',
4751
"dockerfile": '#',
4852
"snippets": '#',
@@ -72,14 +76,7 @@ function isForStatementIgnored() {
7276
return getConfig().supportedLanguageId.ignores.theForStatement;
7377
}
7478

75-
function isUnallowdEndsIncluded(lineText: string) {
76-
let pos = lineText.lastIndexOf(commentDelimiter);
77-
78-
// it it commented
79-
if (pos >= 0) {
80-
lineText = lineText.substring(0, pos);
81-
}
82-
79+
function isUnallowedEndsIncluded(lineText: string) {
8380
let lastCharacter = lineText[lineText.length - 1];
8481

8582
let unallowedEnds = getConfig().autoInsertSemicolon.unallowedEnds.split(",");
@@ -132,6 +129,11 @@ function semicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEditor
132129
function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEditorEdit, forceToEnd: boolean) {
133130
const selections: vscode.Selection[] = [];
134131
const languageId = vscode.window.activeTextEditor?.document.languageId;
132+
133+
autoSemicolonFormatsIncluded = isAutoSemicolonLanguageIdIncluded(languageId);
134+
autoMoveFormatsIncluded = isAutoMoveLanguageIdIncluded(languageId);
135+
commentDelimiter = getCommentDelimiter(languageId);
136+
135137
editor.edit(() => {
136138
try {
137139
editor.selections.forEach((selection) => {
@@ -142,10 +144,7 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
142144
let position: vscode.Position;
143145
let match;
144146

145-
autoSemicolonFormatsIncluded = isAutoSemicolonLanguageIdIncluded(languageId);
146-
autoMoveFormatsIncluded = isAutoMoveLanguageIdIncluded(languageId);
147-
commentDelimiter = getCommentDelimiter(languageId);
148-
147+
commentDelimiterIndex = indexTagOutOfQuotes(lineText, commentDelimiter);
149148
position = newPosition(line, selection.active.character + 1);
150149

151150
if (lineText.length === 0 || !languageId) {
@@ -154,7 +153,7 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
154153
} else if (!autoSemicolonFormatsIncluded && !autoMoveFormatsIncluded) {
155154
textEdit.insert(selection.active, ';');
156155

157-
} else if (!forceToEnd && isCommented(lineText, currentPos)) {
156+
} else if (!forceToEnd && commentDelimiterIndex >= 0 && commentDelimiterIndex < currentPos) {
158157
textEdit.insert(selection.active, ';');
159158

160159
} else if (!forceToEnd && isQuotesIgnored() && isInStringLiteral(lineText, currentPos)) {
@@ -174,7 +173,7 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
174173
let length = line.range.end.character + 1;
175174
if (isBetweenTags('{', '}', lineText, currentPos)) {
176175
length = putSemicolonBefore('}', textEdit, selection, line);
177-
} else if (!isUnallowdEndsIncluded(lineText) || currentPos === line.text.length) {
176+
} else if (!isUnallowedEndsIncluded(lineText) || currentPos === line.text.length) {
178177
length = autoSemicolonBeforeComment(textEdit, selection, line) + 1;
179178
}
180179

@@ -183,17 +182,20 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
183182
} else {
184183
// just move to the end
185184
let length = line.range.end.character + 1;
185+
if (autoMoveFormatsIncluded) {
186+
length = lineText.substring(0, commentDelimiterIndex).trimEnd().length;
187+
}
186188
position = newPosition(line, length);
187189
}
188190

189191
selection = new vscode.Selection(position, position);
190192
selections.push(selection);
191-
} catch (error) {
192-
// logIt(error.message);
193+
} catch (error: any) {
194+
logIt(error.message);
193195
}
194196
});
195-
} catch (error) {
196-
// logIt(error.message);
197+
} catch (error: any) {
198+
logIt(error.message);
197199
}
198200
}).then(() => {
199201
editor.selections = selections;
@@ -215,7 +217,7 @@ function putSemicolonBefore(tag: string, textEdit: vscode.TextEditorEdit, select
215217
const lineTextTrimmed = lineText.substring(0, posClose).trimEnd();
216218
length = lineTextTrimmed.length;
217219

218-
if (!isUnallowdEndsIncluded(lineTextTrimmed) || currentPos === line.text.length) {
220+
if (!isUnallowedEndsIncluded(lineTextTrimmed) || currentPos === line.text.length) {
219221
lineText = lineText.replace(lineTextTrimmed, lineTextTrimmed + ';');
220222
length += 1;
221223
textEdit.delete(new vscode.Selection(newPosition(line, 0), newPosition(line, line.text.length)));
@@ -234,14 +236,13 @@ function putSemicolonBefore(tag: string, textEdit: vscode.TextEditorEdit, select
234236
function autoSemicolonBeforeComment(textEdit: vscode.TextEditorEdit, selection: vscode.Selection, line: vscode.TextLine, justMove: boolean = false): number {
235237
let lineText = line.text.trimEnd();
236238
const currentPos = selection.active.character;
237-
const posClose = lineText.indexOf(commentDelimiter, currentPos);
238239
let length = lineText.length;
239240

240-
if (posClose >= 0) {
241-
const lineTextTrimmed = lineText.substring(0, posClose).trimEnd();
241+
if (commentDelimiterIndex >= 0) {
242+
const lineTextTrimmed = lineText.substring(0, commentDelimiterIndex).trimEnd();
242243
length = lineTextTrimmed.length;
243244

244-
if (!isUnallowdEndsIncluded(lineTextTrimmed) || currentPos === length) {
245+
if (!isUnallowedEndsIncluded(lineTextTrimmed) || currentPos === length) {
245246
lineText = lineText.replace(lineTextTrimmed, lineTextTrimmed + ';');
246247
length += 1;
247248
textEdit.delete(new vscode.Selection(newPosition(line, 0), newPosition(line, line.text.length)));
@@ -294,8 +295,27 @@ function getCommentDelimiter(languageId: string | undefined): string {
294295
return delimiterDefault;
295296
}
296297

297-
function isCommented(lineText: string, currentPos: number): boolean {
298-
return lineText.lastIndexOf(commentDelimiter, currentPos) >= 0;
298+
function indexTagOutOfQuotes(line: string, tag: string): number {
299+
const regex = new RegExp(`${commentDelimiter}(?!['"\`]\S)`, 'g');
300+
const matches = [];
301+
302+
let match;
303+
while ((match = regex.exec(line)) !== null) {
304+
// Check the preceding characters
305+
const precedingChars = line.slice(0, match.index);
306+
const quoteCount = (precedingChars.match(/['"`]/g) || []).length;
307+
308+
// If the count of quotes is even, add the match
309+
if (quoteCount % 2 == 0) {
310+
matches.push(match);
311+
}
312+
}
313+
314+
if (matches.length == 0) {
315+
return -1;
316+
}
317+
318+
return matches[0].index;
299319
}
300320

301321
function findTheForStatement(lineText: string, currentPos: number): string[] | null {
@@ -338,9 +358,11 @@ function isEmpty(value: string | any[] | null | undefined): boolean {
338358
return (value == null || value.length === 0);
339359
}
340360

341-
async function logIt(message: string | any[] | null | undefined) {
342-
console.log(message);
343-
// vscode.window.showWarningMessage(message?.toString());
361+
async function logIt(message: string | any | any[] | null | undefined) {
362+
if (devMode) {
363+
console.log(message);
364+
// vscode.window.showWarningMessage(message?.toString());
365+
}
344366
}
345367

346368
async function taskChecker(context: vscode.ExtensionContext) {

0 commit comments

Comments
 (0)