Skip to content

Commit

Permalink
fix(vls): suppress extra log when VLS initial report
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jun 13, 2021
1 parent c9f02c1 commit 4918762
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/checker-vls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"commander": "^7.2.0",
"log-update": "^4.0.0",
"vite-plugin-checker": "workspace:*",
"vls": "^0.7.2"
"vls": "^0.7.2",
"vscode-languageserver-textdocument": "^1.0.1"
},
"peerDependencies": {
"vite": "^2.0.0"
Expand Down
41 changes: 28 additions & 13 deletions packages/checker-vls/src/commands/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import { codeFrameColumns } from '@babel/code-frame'

import { getInitParams } from '../initParams'

let muteWatchNotification = true
import type { TextDocument } from 'vscode-languageserver-textdocument'
enum DOC_VERSION {
init = -1,
}

export type LogLevel = typeof logLevels[number]
export const logLevels = ['ERROR', 'WARN', 'INFO', 'HINT'] as const
const logLevel2Severity = {
Expand Down Expand Up @@ -77,13 +81,14 @@ export async function diagnostics(
}

// initial report
console.log(os.EOL)
if (!errCount) {
console.log(chalk.green(`VTI found no error`))
console.log(chalk.green(`[VLS checker] No error found`))
if (!watch) {
process.exit(0)
}
} else {
console.log(chalk.red(`VTI found ${errCount} ${errCount === 1 ? 'error' : 'errors'}`))
console.log(chalk.red(`[VLS checker] Found ${errCount} ${errCount === 1 ? 'error' : 'errors'}`))
if (!watch) {
process.exit(1)
}
Expand Down Expand Up @@ -124,8 +129,7 @@ async function prepareClientConnection(workspaceUri: URI, options: DiagnosticOpt

// hijack sendDiagnostics
serverConnection.sendDiagnostics = (diagnostics) => {
if (muteWatchNotification) return

if (diagnostics.version === DOC_VERSION.init) return
if (!diagnostics.diagnostics.length) {
prettyDiagnosticsLog({
ds: [],
Expand All @@ -147,6 +151,18 @@ async function prepareClientConnection(workspaceUri: URI, options: DiagnosticOpt

const vls = new VLS(serverConnection as any)

vls.validateTextDocument = async (textDocument: TextDocument, cancellationToken?: any) => {
const diagnostics = await vls.doValidate(textDocument, cancellationToken)
if (diagnostics) {
// @ts-ignore
vls.lspConnection.sendDiagnostics({
uri: textDocument.uri,
version: textDocument.version,
diagnostics,
})
}
}

serverConnection.onInitialize(async (params: InitializeParams): Promise<InitializeResult> => {
await vls.init(params)

Expand Down Expand Up @@ -220,31 +236,32 @@ async function getDiagnostics(
textDocument: {
languageId: 'vue',
uri: URI.file(absFilePath).toString(),
version: 1,
version: DOC_VERSION.init,
text: fileText,
},
})

try {
let res = (await clientConnection.sendRequest('$/getDiagnostics', {
let diagnostics = (await clientConnection.sendRequest('$/getDiagnostics', {
uri: URI.file(absFilePath).toString(),
version: DOC_VERSION.init,
})) as Diagnostic[]
/**
* Ignore eslint errors for now
*/
res = res
diagnostics = diagnostics
.filter((r) => r.source !== 'eslint-plugin-vue')
.filter((r) => r.severity && r.severity <= severity)

if (res.length > 0) {
if (diagnostics.length > 0) {
logChunk += prettyDiagnosticsLog({
absFilePath,
fileText,
ds: res,
ds: diagnostics,
doLog: false,
})

res.forEach((d) => {
diagnostics.forEach((d) => {
if (d.severity === DiagnosticSeverity.Error) {
initialErrCount++
}
Expand All @@ -254,9 +271,7 @@ async function getDiagnostics(
console.error(err.stack)
}
}

logUpdate(logChunk)
muteWatchNotification = false
return initialErrCount
}

Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 4918762

Please sign in to comment.