diff --git a/TODO b/TODO index c5f780f..1b26ffc 100644 --- a/TODO +++ b/TODO @@ -1,30 +1,27 @@ MVP === - lint everything -- [v] lint on save -- settings - - max error level - delete lint on close file - reasonable code layout TODO ==== - lint current file - [v] command clear all issues Long Term Desired Features ========================== - Fancy UIs for - arc patch - arc todo - arc unit - arc browse - arc paste (upload and download) - list open Differential in repo (for review) - get hovercard (content) for any object - push notifications from website ? - preview Remarkup - some magic to help the External Editor Link - arc lint: special-case some messages for better visibility (find more range/information.) - arc lint: support auto-fix (apply diff) - arc lint: `locations` field may be parsed into `relatedInformation`. diff --git a/package.json b/package.json index 067d1ca..3cf421b 100644 --- a/package.json +++ b/package.json @@ -1,49 +1,60 @@ { "name": "arc-vscode", "displayName": "arc vscode", "description": "Arcanist (Phabricator) support for VSCode", "version": "0.0.1", "engines": { "vscode": "^1.46.0" }, "categories": [ "Other" ], "activationEvents": [ "workspaceContains:**/.arclint", "workspaceContains:**/.arcconfig" ], "main": "./out/extension.js", "contributes": { "commands": [ { "command": "arc-vscode.clearLint", "title": "Clear all arc-lint messages" } - ] + ], + "configuration": { + "title": "Arcanist", + "properties": { + "arc-vscode.lint.maxDiagnosticsLevel": { + "type": "string", + "default": "error", + "enum": ["error", "warning", "info", "hint"], + "description": "The maximum level a lint can appear at." + } + } + } }, "scripts": { "vscode:prepublish": "npm run compile", "compile": "tsc -p ./", "lint": "eslint src --ext ts", "watch": "tsc -watch -p ./", "pretest": "npm run compile && npm run lint", "test": "node ./out/test/runTest.js" }, "devDependencies": { "@types/vscode": "^1.46.0", "@types/glob": "^7.1.1", "@types/mocha": "^7.0.2", "@types/node": "^13.11.0", "eslint": "^6.8.0", "@typescript-eslint/parser": "^2.30.0", "@typescript-eslint/eslint-plugin": "^2.30.0", "glob": "^7.1.6", "mocha": "^7.1.2", "typescript": "^3.8.3", "vscode-test": "^1.3.0" }, "dependencies": { "execa": "^4.0.2" } } diff --git a/src/extension.ts b/src/extension.ts index 4bf84a5..954ccf1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,193 +1,218 @@ import * as vscode from 'vscode'; import * as execa from "execa"; import * as path from "path"; import { ArcanistLintMessage } from './arcanist_types'; export function activate(context: vscode.ExtensionContext) { const collection = vscode.languages.createDiagnosticCollection('arc lint'); setupCustomTranslators(); + updateLintSeverityMap(); function d(disposable: vscode.Disposable) { context.subscriptions.push(disposable); } d(vscode.commands.registerCommand('arc-vscode.clearLint', () => collection.clear())); d(vscode.workspace.onDidSaveTextDocument(onTextDocumentEvent)); d(vscode.workspace.onDidOpenTextDocument(onTextDocumentEvent)); + d(vscode.workspace.onDidChangeConfiguration(onChangeConfig)); if (vscode.window.activeTextEditor) { lintFile(vscode.window.activeTextEditor.document); } d(vscode.window.onDidChangeActiveTextEditor(editor => { if (editor) { lintFile(editor.document); } })); function onTextDocumentEvent(document: vscode.TextDocument) { lintFile(document); } function lintFile(document: vscode.TextDocument) { if (document.uri.scheme != "file") return; function handleExecResult(value: execa.ExecaReturnValue) { if (!value.stdout) return; try { const lintMessages = JSON.parse(value.stdout); for (const filename in lintMessages) { // TODO: This only probably works because we call arc with a single file. collection.set(document.uri, lintJsonToDiagnostics(lintMessages[filename])); } } catch { logError("ppfff"); } } const filename = document.uri.path; execa( 'arc', ['lint', '--output', 'json', '--', path.basename(filename)], { cwd: path.dirname(filename) }, ).then(handleExecResult, handleExecResult); } } export function deactivate() { // TODO collection.clear(); - } +} + +function onChangeConfig(e: vscode.ConfigurationChangeEvent) { + if (!e.affectsConfiguration('arc-vscode.lint')) { + return; + } + updateLintSeverityMap(); +} function logError(x: any) { console.log("this is error", x); } type LintTranslator = (lint: ArcanistLintMessage) => vscode.Diagnostic; let customLintTranslator: Map = new Map(); function lintJsonToDiagnostics(lintResults: Array): vscode.Diagnostic[] { /* input: Array of: { "line": 248, "char": 23, "code": "SPELL1", "severity": "warning", "name": "Possible Spelling Mistake", "description": "Possible spelling error. You wrote 'seperator', but did you mean 'separator'?", "original": "Seperator", "replacement": "Separator", "granularity": 1, "locations": [], "bypassChangedLineFiltering": null, "context": " magic = COLOR_RED;\n break;\n case 30:\n // printf(\"Record Seperator\");\n magic = COLOR_BLUE;\n break;\n case 31:" } output: array of: { code: '', message: 'cannot assign twice to immutable variable `x`', range: new vscode.Range(new vscode.Position(3, 4), new vscode.Position(3, 10)), severity: vscode.DiagnosticSeverity.Error, source: '', relatedInformation: [ new vscode.DiagnosticRelatedInformation(new vscode.Location(document.uri, new vscode.Range(new vscode.Position(1, 8), new vscode.Position(1, 9))), 'first assignment to `x`') ] } */ /* Extra features: - quick-fix to apply patch - try to get better message by parsing `description` field (per message code...) - `locations` may be parsed into `relatedInformation`. */ - - - function translate(lint: ArcanistLintMessage): vscode.Diagnostic { let t = customLintTranslator.get(lint.code) || defaultTranslate; return t(lint); } return lintResults.map(translate); } function nonNeg(n: number): number { return n < 0 ? 0 : n } +let lintSeverityMap: Map; +function updateLintSeverityMap(): void { + let config = vscode.workspace.getConfiguration('arc-vscode.lint'); + let maxLevel: vscode.DiagnosticSeverity; + switch (config.maxDiagnosticsLevel as string) { + case 'hint': maxLevel = vscode.DiagnosticSeverity.Hint; break; + case 'info': maxLevel = vscode.DiagnosticSeverity.Information; break; + case 'warning': maxLevel = vscode.DiagnosticSeverity.Warning; break; + case 'error': + default: + maxLevel = vscode.DiagnosticSeverity.Error; break; + } + + function capped(level: vscode.DiagnosticSeverity): vscode.DiagnosticSeverity { + return level > maxLevel ? level : maxLevel; + } + + lintSeverityMap = new Map(); + lintSeverityMap.set('disabled', capped(vscode.DiagnosticSeverity.Hint)) + lintSeverityMap.set('autofix', capped(vscode.DiagnosticSeverity.Information)) + lintSeverityMap.set('advice', capped(vscode.DiagnosticSeverity.Information)) + lintSeverityMap.set('warning', capped(vscode.DiagnosticSeverity.Warning)) + lintSeverityMap.set('error', capped(vscode.DiagnosticSeverity.Error)) +} + +function severity(lint: ArcanistLintMessage): vscode.DiagnosticSeverity { + return lintSeverityMap.get(lint.severity as string) || vscode.DiagnosticSeverity.Error; +} + function defaultTranslate(lint: ArcanistLintMessage): vscode.Diagnostic { return { code: lint.code, message: message(lint), severity: severity(lint), source: 'arc lint', range: new vscode.Range( lint.line - 1, nonNeg(lint.char - 2), // it's an artificial 3-chars wide thing. lint.line - 1, lint.char + 1), }; } function message(lint: ArcanistLintMessage) { if (lint.description) return lint.name + ": " + lint.description return lint.name } -function severity(lint: ArcanistLintMessage): vscode.DiagnosticSeverity { - switch (lint.severity as string) { - case 'disabled': return vscode.DiagnosticSeverity.Hint; - case 'autofix': return vscode.DiagnosticSeverity.Hint; - case 'advice': return vscode.DiagnosticSeverity.Information; - case 'warning': return vscode.DiagnosticSeverity.Warning; - case 'error': return vscode.DiagnosticSeverity.Error; - default: return vscode.DiagnosticSeverity.Error; - } -} function setupCustomTranslators() { customLintTranslator.set("SPELL1", lint => { let d = defaultTranslate(lint) d.message = lint.description; let len = (lint.original).length; if (len > 0) { d.range = new vscode.Range( lint.line - 1, nonNeg(lint.char - 1), lint.line - 1, lint.char + len - 1); } return d }); // "This line is 116 characters long, but the convention is 80 characters." const re_TXT3_length = /\D(\d+) characters\.$/; customLintTranslator.set('E501', lint => { let d = defaultTranslate(lint) d.range = new vscode.Range( lint.line - 1, lint.char - 1, lint.line - 1, 1e9); return d; }); customLintTranslator.set('TXT3', lint => { let d = defaultTranslate(lint) let match = (lint.description).match(re_TXT3_length); if (match) { let len = parseInt(match[1]); d.range = new vscode.Range( lint.line - 1, len, lint.line - 1, 1e9); } return d; }); }