Skip to content

Commit

Permalink
feat: auto remove semicolon (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilanaya authored Jun 7, 2022
1 parent 3306fb9 commit 4679624
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@
"type": "boolean",
"default": true
},
"autoRemoveSemicolon.enable": {
"type": "boolean",
"default": true
},
"typeDecorations.languages": {
"type": "array",
"items": {
Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { registerSelectLineContents } from './features/selectLineContents'
import { registerCutLineContents } from './features/cutLineContents'
import { registerCutLineContentsPreserve } from './features/cutLineContentsPreserve'
import typeDecorations from './features/typeDecorations'
import autoRemoveSemicolon from './features/autoRemoveSemicolon'

export const activate = () => {
// preserve camelcase identifiers (only vars for now)
Expand Down Expand Up @@ -76,6 +77,7 @@ export const activate = () => {
registerCutLineContents()
registerCutLineContentsPreserve()
typeDecorations()
autoRemoveSemicolon()

// vscode.languages.registerSelectionRangeProvider('*', {
// provideSelectionRanges(document, positions, token) {
Expand Down
45 changes: 45 additions & 0 deletions src/features/autoRemoveSemicolon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as vscode from 'vscode'
import { equals } from 'rambda'
import { defaultJsSupersetLangsWithVue } from '@zardoy/vscode-utils/build/langs'

export default () => {
vscode.workspace.onDidChangeTextDocument(async ({ document, contentChanges }) => {
const textEditor = vscode.window.activeTextEditor

if (
document.uri !== textEditor?.document.uri ||
textEditor.viewColumn === undefined ||
!vscode.languages.match(defaultJsSupersetLangsWithVue, document)
)
return
const line = document.lineAt(contentChanges[0]!.range.end)
if (
equals(
contentChanges.map(({ text }) => text),
['.'],
) &&
/;.$/.test(line.text)
) {
await textEditor.edit(
editBuilder => {
const endPos = contentChanges[0]!.range.end
editBuilder.delete(new vscode.Range(endPos.translate(0, -1), endPos))
},
{ undoStopAfter: false, undoStopBefore: false },
)
await vscode.commands.executeCommand('editor.action.triggerSuggest')
}

if (
equals(
contentChanges.map(({ text }) => text),
[')', '('],
) &&
document.lineAt(contentChanges[0]!.range.end).text.endsWith(';)')
)
await textEditor.edit(editBuilder => {
const endPos = document.lineAt(contentChanges[0]!.range.end).range.end.translate(0, -1)
editBuilder.delete(new vscode.Range(endPos.translate(0, -1), endPos))
})
})
}

0 comments on commit 4679624

Please sign in to comment.