Skip to content

Commit

Permalink
feat: support more language's comment. close #12.
Browse files Browse the repository at this point in the history
  • Loading branch information
lvjiaxuan committed Mar 21, 2023
1 parent 5a611ac commit f277664
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ExtensionContext, Position, Range, Selection, SnippetString, commands, languages, window } from 'vscode'
import { ExtensionContext, Position, Range, SnippetString, commands, languages, window } from 'vscode'
import { existFileSync, getTextBylines } from './utils'
import { getBlockComment, getExtension, getLineComment } from './languageDefaults'
import log from './log'
import path from 'node:path'

Expand Down Expand Up @@ -41,22 +42,25 @@ const disableForLines = commands.registerCommand('eslint-disable.disable', () =>
return insertRules
}, new Set<string>())

const lineComment = getLineComment(activeTextEditor.document.languageId)
const blockComment = getBlockComment(activeTextEditor.document.languageId)

if (selection.isSingleLine) {
// Insert at previous line.
void activeTextEditor.insertSnippet(
new SnippetString(`// eslint-disable-next-line \${1:${ [ ...insertRules ].join(', ') }}\n`),
new SnippetString(`${ lineComment } eslint-disable-next-line \${1:${ [ ...insertRules ].join(', ') }}\n`),
new Position(selection.start.line, insertIndex),
)
} else {
// Wrap lines and Press `ctrl+d` to edit rules between lines.

void (async () => {
await activeTextEditor.insertSnippet(
new SnippetString(`${ ' '.repeat(insertIndex) }/* eslint-enable ${ [ ...insertRules ].join(', ') } */\n`),
new SnippetString(`${ ' '.repeat(insertIndex) }${ blockComment[0] } eslint-enable ${ [ ...insertRules ].join(', ') } ${ blockComment[1] }\n`),
new Position(selection.end.line + 1, 0),
)
await activeTextEditor.insertSnippet(
new SnippetString(`/* eslint-disable \${1:${ [ ...insertRules ].join(', ') }} */\n`),
new SnippetString(`${ blockComment[0] } eslint-disable \${1:${ [ ...insertRules ].join(', ') }} ${ blockComment[1] }\n`),
new Position(selection.start.line, insertIndex),
)
})()
Expand Down Expand Up @@ -91,6 +95,9 @@ const disableForFile = commands.registerCommand('eslint-disable.entire', async (
const startLineText = getTextBylines(0)
const startLineTextMatch = startLineText?.match(/\/\* eslint-disable (?<rules>.+) \*\//i)

const lineComment = getLineComment(activeTextEditor.document.languageId)
const blockComment = getBlockComment(activeTextEditor.document.languageId)

if (startLineTextMatch) {
const currentRules = startLineTextMatch?.groups!.rules.replaceAll(' ', '').split(',') ?? []
currentRules.forEach(item => insertRules.add(item))
Expand All @@ -104,7 +111,7 @@ const disableForFile = commands.registerCommand('eslint-disable.entire', async (
))

const endLineText = getTextBylines(activeTextEditor.document.lineCount - 2)
if (endLineText?.startsWith('/* eslint-enable')) {
if (endLineText?.startsWith(`${ blockComment[0] } eslint-enable`)) {
editor.delete(new Range(
new Position(activeTextEditor.document.lineCount - 2, 0),
new Position(activeTextEditor.document.lineCount - 2, Number.MAX_SAFE_INTEGER),
Expand All @@ -113,20 +120,20 @@ const disableForFile = commands.registerCommand('eslint-disable.entire', async (
})

await activeTextEditor.insertSnippet(
new SnippetString(`/* eslint-enable ${ [ ...insertRules ].join(', ') } */`),
new SnippetString(`${ blockComment[0] } eslint-enable ${ [ ...insertRules ].join(', ') } ${ blockComment[1] }`),
new Position(activeTextEditor.document.lineCount - 2, 0),
)
await activeTextEditor.insertSnippet(
new SnippetString(`/* eslint-disable ${ [ ...insertRules ].join(', ') } */`),
new SnippetString(`${ blockComment[0] } eslint-disable ${ [ ...insertRules ].join(', ') } ${ blockComment[1] }`),
new Position(0, 0),
)
} else {
await activeTextEditor.insertSnippet(
new SnippetString(`/* eslint-enable ${ [ ...insertRules ].join(', ') } */\n`),
new SnippetString(`${ blockComment[0] } eslint-enable ${ [ ...insertRules ].join(', ') } ${ blockComment[1] }\n`),
new Position(activeTextEditor.document.lineCount + 1, 0),
)
await activeTextEditor.insertSnippet(
new SnippetString(`/* eslint-disable ${ [ ...insertRules ].join(', ') } */\n`),
new SnippetString(`${ blockComment[0] } eslint-disable ${ [ ...insertRules ].join(', ') } ${ blockComment[1] }\n`),
new Position(0, 0),
)
}
Expand All @@ -137,6 +144,7 @@ const disableAllForFile = commands.registerCommand('eslint-disable.all', () => {
void commands.executeCommand('eslint-disable.entire', true)
})


function getESLintDiagnostics() {
const { activeTextEditor } = window

Expand Down
32 changes: 32 additions & 0 deletions src/languageDefaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// https://github.com/Microsoft/vscode-eslint/blob/main/server/src/languageDefaults.ts

type LanguageConfig = {
ext: string;
lineComment: string;
blockComment: [string, string];
}

const languageId2Config: Map<string, LanguageConfig> = new Map([
[ 'javascript', { ext: 'js', lineComment: '//', blockComment: [ '/*', '*/' ] } ],
[ 'javascriptreact', { ext: 'jsx', lineComment: '//', blockComment: [ '/*', '*/' ] } ],
[ 'typescript', { ext: 'ts', lineComment: '//', blockComment: [ '/*', '*/' ] } ],
[ 'typescriptreact', { ext: 'tsx', lineComment: '//', blockComment: [ '/*', '*/' ] } ],
[ 'html', { ext: 'html', lineComment: '//', blockComment: [ '<!--', '-->' ] } ],
[ 'vue', { ext: 'vue', lineComment: '//', blockComment: [ '<!--', '-->' ] } ],
[ 'coffeescript', { ext: 'coffee', lineComment: '#', blockComment: [ '###', '###' ] } ],
[ 'yaml', { ext: 'yaml', lineComment: '#', blockComment: [ '#', '' ] } ],
[ 'graphql', { ext: 'graphql', lineComment: '#', blockComment: [ '#', '' ] } ],
])

export function getLineComment(languageId: string): string {
return languageId2Config.get(languageId)?.lineComment ?? '//'
}

export function getBlockComment(languageId: string): [string, string] {
return languageId2Config.get(languageId)?.blockComment ?? [ '/**', '*/' ]
}

export function getExtension(languageId: string): string | undefined {
return languageId2Config.get(languageId)?.ext
}

0 comments on commit f277664

Please sign in to comment.