Skip to content

Commit

Permalink
feat(lint): add notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
lvjiaxuan committed Sep 1, 2022
1 parent eb31e9b commit c1aabcf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 25 deletions.
79 changes: 55 additions & 24 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { type ExtensionContext, Position, SnippetString, commands, window, workspace } from 'vscode'
import { type ExtensionContext, Position, ProgressLocation, SnippetString, TextEdit, TextEditor, commands, window, workspace } from 'vscode'
import log from './log'
import { ESLint } from 'eslint'
import { getTextBylines } from './utils'


let eslint: ESLint | null

export function activate(context: ExtensionContext) {

log('eslint-disabled activated!')
Expand All @@ -20,7 +21,7 @@ export function activate(context: ExtensionContext) {
},
})

disposes.forEach(dispose => context.subscriptions.push(dispose))
context.subscriptions.push(...disposes)
}

// this method is called when your extension is deactivated
Expand All @@ -30,57 +31,77 @@ export function deactivate() {

const disposes = [

// hello world
commands.registerCommand('eslint-disable.helloWorld', () => {
void window.showInformationMessage('Hello World from eslint-disable!!!')
}),

// disable lines
commands.registerCommand('eslint-disable.disableIT', async () => {

const activeTextEditor = window.activeTextEditor
if (!activeTextEditor || !eslint) {
log('hold on...')
log('no `activeTextEditor` exist.', true)
return
}

log('Start linting...')
// todo spinning on status bar
const results = await eslint.lintFiles(activeTextEditor.document.uri.fsPath)
log('Linting finish...')
const lineRuleIdsMap = results?.[0].messages.reduce((preValue, item) => {
if (!item.ruleId) return preValue
if (!preValue[item.line]) {
preValue[item.line] = [ item.ruleId ]
} else {
preValue[item.line] = [ ...preValue[item.line], item.ruleId ]
}
return preValue
}, {} as Record<number, string[]>) ?? {}
const lineRuleIdsMap = await window.withProgress({
cancellable: false,
location: ProgressLocation.Notification,
title: 'Progress Notification',
}, async (progress, token) => {

log('Start linting hole file content...')
let count = 5
progress.report({ increment: 5, message: 'Start linting hole file content...' })

const fakeIncrement = setInterval(() => {
progress.report({ increment: count++, message: 'Still going...' })
if (count == 95) clearInterval(fakeIncrement)
}, 1000)

const results = await eslint!.lintFiles(activeTextEditor.document.uri.fsPath)

log('Linting finish...')
clearInterval(fakeIncrement)
progress.report({ increment: 99, message: 'Linting finish.' })

const lineRuleIdsMap = results?.[0].messages.reduce((preValue, item) => {
if (!item.ruleId) return preValue
if (!preValue[item.line]) {
preValue[item.line] = [ item.ruleId ]
} else {
preValue[item.line] = [ ...preValue[item.line], item.ruleId ]
}
return preValue
}, {} as Record<number, string[]>) ?? {}

// eslint-disable-next-line no-promise-executor-return
return new Promise<typeof lineRuleIdsMap>(resolve => setTimeout(() => resolve(lineRuleIdsMap), 500))
})

if (!Object.keys(lineRuleIdsMap).length) {
log('Everything is good.')
void window.showInformationMessage('Everything is good.', 'OK')
return
}

activeTextEditor.selections.forEach(selection => {

const text = getTextBylines(selection.start.line, selection.end.line)
if (!text) return
if (!text) {
void window.showInformationMessage('No content to disable.', 'OK')
return
}

let insertIndex = 0
while (text.charAt(insertIndex) == ' ') {
insertIndex++
}

if (selection.isSingleLine) {
// Insert at previous line.
// Insert at previous line.
void activeTextEditor.insertSnippet(
new SnippetString(`// eslint-disable-next-line \${1|${ lineRuleIdsMap[selection.start.line + 1].join('\\, ') }|}\n`),
new Position(selection.start.line, insertIndex),
)
} else {
// wrap lines. Press `ctrl+d `to edit rules.
// wrap lines. Press `ctrl+d `to edit rules.
void activeTextEditor.insertSnippet(
new SnippetString('/* eslint-disable ${1|INSERT_RULES|} */\n'),
new Position(selection.start.line, insertIndex),
Expand All @@ -91,6 +112,16 @@ const disposes = [
)
}
})

}),

// hello world
commands.registerCommand('eslint-disable.helloWorld', () => {
const p = window.showInformationMessage('Hello World from eslint-disable!!!')
void p.then(r => {
return r
})
// ...
}),
]

5 changes: 4 additions & 1 deletion src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ const getNowFormat = () => {
}

export { channel }
export default (message: string) => channel.appendLine(`[${ getNowFormat() }] - ${ message }`)
export default (message: string, showInformationMessage = false) => {
channel.appendLine(`[${ getNowFormat() }] - ${ message }`)
showInformationMessage && window.showInformationMessage(message)
}

0 comments on commit c1aabcf

Please sign in to comment.