Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Insert Missing Comma on Enter #6

Merged
merged 47 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
b075388
Add "Insert Comma"
AgentRBY Nov 19, 2022
e642ff6
Update docs
AgentRBY Nov 19, 2022
261fad1
Fix typo
AgentRBY Nov 19, 2022
876d6f8
Back version
AgentRBY Nov 20, 2022
cda655d
Rename to insertMissingCommaOnEnter
AgentRBY Nov 20, 2022
e269fa3
Advanced support JSONC
AgentRBY Nov 20, 2022
5ce702d
Add multicursor support
AgentRBY Nov 20, 2022
71347a2
fix eslint issues
zardoy Nov 20, 2022
7734873
simplify code
zardoy Nov 20, 2022
7dc2c58
fix incorrect multicursor insertion
zardoy Nov 20, 2022
4810aad
Merge remote-tracking branch 'upstream/main'
AgentRBY Nov 21, 2022
e0760ab
Fix isNumber
AgentRBY Nov 21, 2022
c75289b
Move condition to upperScope
AgentRBY Nov 21, 2022
a9b0db7
Add support comments in classic JSON
AgentRBY Nov 21, 2022
08dfcd6
Add checking reason
AgentRBY Nov 21, 2022
da41b75
Add undoStopAfter and undoStopBefore
AgentRBY Nov 21, 2022
b2248e9
Add support Inserting comma before comment
AgentRBY Nov 21, 2022
f9470e4
Fix typo
AgentRBY Nov 21, 2022
b5b386a
Refactor and perfomance
AgentRBY Nov 21, 2022
162fb3d
Merge branch 'main' of https://github.com/AgentRBY/vscode-fix-all-json
AgentRBY Nov 21, 2022
77aedc6
Support inserting in end of comment
AgentRBY Nov 21, 2022
3a84ad1
Minor fix
AgentRBY Nov 21, 2022
fb26614
Replace trimEnd to trim
AgentRBY Nov 21, 2022
509c76f
Remove "whitespace: false"
AgentRBY Nov 21, 2022
eb659c0
Update src/extension.ts
AgentRBY Nov 21, 2022
db0c26e
Fix
AgentRBY Nov 21, 2022
2b6ac45
Replcae to ??=
AgentRBY Nov 21, 2022
fa96be2
Refactoring
AgentRBY Nov 21, 2022
06c0c86
Merge remote-tracking branch 'upstream/main'
AgentRBY Nov 22, 2022
12c5e42
Inline var
AgentRBY Nov 22, 2022
04989c8
Update package.json
AgentRBY Nov 22, 2022
f02eb7a
Move to commaOnEnter and prettier all
AgentRBY Nov 22, 2022
9a0f16d
Prettier
AgentRBY Nov 22, 2022
534611f
add comma on enter integration tests
zardoy Nov 22, 2022
73ba1eb
fix tests
zardoy Nov 22, 2022
b01fa7e
style: merge some if-return statements to reduce number of lines
zardoy Nov 22, 2022
20af42e
Merge branch 'main' into pr/AgentRBY/6
zardoy Nov 22, 2022
9fd0928
update setting description (again)
zardoy Nov 22, 2022
455461b
ci: try to delay tests
zardoy Nov 22, 2022
7bedc55
workaround vscode builtin JSON bug
zardoy Nov 22, 2022
349988a
Merge branch 'main' into pr/AgentRBY/6
zardoy Nov 22, 2022
7ebb839
test: avoid falsey triggers because of other test files
zardoy Nov 23, 2022
6eb4691
ci: try to ignore empty diagnostics update?
zardoy Nov 23, 2022
3e41c71
Add more tests
AgentRBY Nov 23, 2022
db82251
Change EoL checking
AgentRBY Nov 23, 2022
fd8439f
Format
AgentRBY Nov 23, 2022
bc4451b
adjust tests
zardoy Nov 23, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
"runOnSave": {
"type": "boolean",
"default": true
},
"insertMissingCommaOnEnter": {
"type": "boolean",
"default": true,
"description": "Insert missing comma after \"Enter\". Working only if last symbol is `}`, `]`, `\"` or number"
AgentRBY marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -107,6 +112,7 @@
"typescript": "^4.5.2"
},
"dependencies": {
"strip-json-comments": "^5.0.0",
"vscode-framework": "^0.0.18"
}
}
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

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

88 changes: 88 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

import * as vscode from 'vscode'
import { getExtensionSetting, registerExtensionCommand } from 'vscode-framework'
import stripJsonComments from 'strip-json-comments'
import { getTextByLine, isNumber } from './utils';


export const activate = () => {
vscode.languages.registerCodeActionsProvider(
Expand Down Expand Up @@ -110,4 +113,89 @@ export const activate = () => {
if (!getExtensionSetting('runOnSave') || reason === vscode.TextDocumentSaveReason.AfterDelay) return
waitUntil(performFixes())
})

vscode.workspace.onDidChangeTextDocument(
AgentRBY marked this conversation as resolved.
Show resolved Hide resolved
({ contentChanges, document }) => {
AgentRBY marked this conversation as resolved.
Show resolved Hide resolved
if (!getExtensionSetting("insertMissingCommaOnEnter")) {
return;
}

if (
document.languageId !== "json" &&
document.languageId !== "jsonc"
) {
return;
}

if (contentChanges.length === 0) {
return;
}

const editor = vscode.window.activeTextEditor;

if (
document.uri !== editor?.document.uri ||
["output"].includes(editor.document.uri.scheme)
) {
return;
}
if (
vscode.workspace.fs.isWritableFileSystem(
document.uri.scheme
) === false
) {
return;
}

editor.edit((edit) => {
contentChanges.forEach(async (content) => {
AgentRBY marked this conversation as resolved.
Show resolved Hide resolved
if (
!content.text.startsWith("\n") &&
zardoy marked this conversation as resolved.
Show resolved Hide resolved
!content.text.startsWith("\r\n")
) {
return;
}

const prevLinePosition = document.positionAt(content.rangeOffset);

const prevLine = document.lineAt(prevLinePosition);
const prevLineText = prevLine.text;

const prevLineLastChar = prevLineText.at(-1);

if (!prevLineLastChar) {
return;
}

if (document.languageId === 'jsonc') {
AgentRBY marked this conversation as resolved.
Show resolved Hide resolved
if (prevLineText.trim().startsWith("//") || prevLineText.trim().startsWith("/*")) {
return;
}

const textWithouComments = stripJsonComments(document.getText());
const prevLineTextWithoutComments = getTextByLine(textWithouComments, prevLine.lineNumber)?.trim();

if (prevLineTextWithoutComments !== prevLineText.trim()) {
return;
}
}

const isCurrentLineEmpty =
document.lineAt(prevLine.lineNumber + 1).text.trim() === "";

const isMatchValue =
prevLineLastChar === "}" ||
prevLineLastChar === '"' ||
prevLineLastChar === ']' ||
isNumber(prevLineLastChar);

if (isMatchValue && isCurrentLineEmpty) {
// In multicursor mode last character position is broken when I use prevLinePosition, IDK why
AgentRBY marked this conversation as resolved.
Show resolved Hide resolved
const lastCharacterPosition = new vscode.Position(prevLine.lineNumber, prevLine.range.end.character);
edit.insert(lastCharacterPosition, ",");
}
})
});
AgentRBY marked this conversation as resolved.
Show resolved Hide resolved
}
);
}
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function getTextByLine(text:string, line: number) {
return text.split("\n").at(line);
}

export function isNumber(text: string) {
return !Number.isNaN(Number(text));
AgentRBY marked this conversation as resolved.
Show resolved Hide resolved
}