diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 8dd6981..48e033e 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -26,6 +26,6 @@ jobs: with: node-version: ${{ matrix.node-version }} cache: 'npm' - - run: npm ci + - run: npm ci --force - run: npm run compile - run: npm run lint diff --git a/.gitignore b/.gitignore index 004573d..25996fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ +*.tgz out node_modules write-good-linter-*.vsix .DS_Store -test.md \ No newline at end of file +test.md diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..6875b6f --- /dev/null +++ b/.npmignore @@ -0,0 +1,3 @@ +/* +!/out/ +*.map diff --git a/README.md b/README.md index 7207663..7a45201 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,25 @@ # Write Good Linter for Visual Studio Code -Provides a [write-good](https://github.com/btford/write-good) linter extension for [Visual Studio Code](https://code.visualstudio.com/). +Provides a [write-good](https://github.com/btford/write-good) linter extension for [Visual Studio Code](https://code.visualstudio.com/) +and (Neo)Vim. ## Installation +### (Neo)Vim + +- [coc-marketplace](https://github.com/fannheyward/coc-marketplace) +- [npm](https://www.npmjs.com/package/write-good-linter) +- vim: + +```vim +" command line +CocInstall write-good-linter +" or add the following code to your vimrc +let g:coc_global_extensions = ['write-good-linter', 'other coc-plugins'] +``` + +### VS Code + Press F1 or CTRL+P (or CMD+P) and type out `> ext install travisthetechie.write-good-linter`. Check out the latest published version on the [Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=travisthetechie.write-good-linter). ## Settings diff --git a/eslint.config.mjs b/eslint.config.mjs index 2d8d8e7..ac4b19b 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -32,4 +32,4 @@ export default [ "@typescript-eslint/no-non-null-assertion": 0, }, }, -]; \ No newline at end of file +]; diff --git a/package-lock.json b/package-lock.json index 002b637..161e2de 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,10 +20,12 @@ "@types/write-good": "1.0.3", "@typescript-eslint/eslint-plugin": "^8.15.0", "@typescript-eslint/parser": "^8.15.0", + "coc.nvim": "^0.0.83-next.18", "eslint": "^9.15.0", "typescript": "^5.7.2" }, "engines": { + "coc": "^0.0.82", "vscode": "^1.95.0" } }, @@ -624,6 +626,23 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, + "node_modules/coc.nvim": { + "version": "0.0.83-next.18", + "resolved": "https://registry.npmjs.org/coc.nvim/-/coc.nvim-0.0.83-next.18.tgz", + "integrity": "sha512-BZ1a+lnvQ/l7WzoRy97oYubDnYkX+vyAN2GhrKzWMc4d8YujOcOCjaeRv6Ogn7Bvoc5rM9X88CPuOoE0iP1ceQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=16.18.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/cocnvim" + }, + "peerDependencies": { + "@types/node": "^16.18.0" + } + }, "node_modules/color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", diff --git a/package.json b/package.json index 9914584..da909fb 100644 --- a/package.json +++ b/package.json @@ -5,15 +5,19 @@ "version": "0.1.6", "publisher": "travisthetechie", "engines": { + "coc": "^0.0.82", "vscode": "^1.95.0" }, + "keywords": [ + "coc.nvim" + ], "categories": [ "Linters" ], "activationEvents": [ - "onStartupFinished" + "*" ], - "main": "./out/src/extension", + "main": "./out/src/extension.js", "contributes": { "configuration": { "type": "object", @@ -75,6 +79,7 @@ "@eslint/js": "^9.15.0", "@types/node": "^22.9.3", "@types/vscode": "1.95.0", + "coc.nvim": "^0.0.83-next.18", "@types/write-good": "1.0.3", "@typescript-eslint/eslint-plugin": "^8.15.0", "@typescript-eslint/parser": "^8.15.0", diff --git a/src/extension.ts b/src/extension.ts index b1c983c..44147d4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,5 +1,30 @@ -import { workspace, ExtensionContext, TextDocument, languages, Uri, - Diagnostic, DiagnosticCollection } from 'vscode'; +import type { + ExtensionContext as ExtensionContext_vscode, + TextDocument as TextDocument_vscode, + Diagnostic as Diagnostic_vscode, + DiagnosticCollection as DiagnosticCollection_vscode, +} from 'vscode'; +import type { + ExtensionContext as ExtensionContext_coc, + TextDocument as TextDocument_coc, + Diagnostic as Diagnostic_coc, + DiagnosticCollection as DiagnosticCollection_coc, +} from 'coc.nvim'; +type ExtensionContext = ExtensionContext_vscode | ExtensionContext_coc; +type TextDocument = TextDocument_vscode | TextDocument_coc; +type Diagnostic = Diagnostic_vscode | Diagnostic_coc; +type DiagnosticCollection = DiagnosticCollection_vscode | DiagnosticCollection_coc; +let vscode; +try { + vscode = require('vscode'); // eslint-disable-line +} catch (error) { + vscode = require('coc.nvim'); // eslint-disable-line +} +const workspace = vscode.workspace; +const languages = vscode.languages; +const Uri = vscode.Uri; + + import { lintText } from './linter'; let diagnosticCollection: DiagnosticCollection; @@ -27,10 +52,25 @@ export function activate(context: ExtensionContext) { } })); + function didOpenTextDocument(document: TextDocument) { + if (isWriteGoodLanguage(document.languageId)) { + doLint(document); + } + } + + context.subscriptions.push(workspace.onDidOpenTextDocument(didOpenTextDocument)); + workspace.documents.map((doc) => { + didOpenTextDocument(doc.textDocument); + }); + // attempt to only lint changes on motification context.subscriptions.push(workspace.onDidChangeTextDocument(event => { - if (!isWriteGoodLanguage(event.document.languageId)) { - // language is unsupported. + try { + if (!isWriteGoodLanguage(event.document.languageId)) { + // language is unsupported. + return; + } + } catch (error) { return; } @@ -75,7 +115,13 @@ function resetDiagnostics() { diagnosticCollection.clear(); diagnosticMap.forEach((diags, file) => { - diagnosticCollection.set(Uri.parse(file), diags); + try { + // @ts-expect-error: for vscode + diagnosticCollection.set(Uri.parse(file), diags); + } catch (e) { + // @ts-expect-error: for coc.nvim + diagnosticCollection.set(Uri.parse(file).path, diags); + } }); } @@ -95,4 +141,4 @@ function doLint(document: TextDocument) { diagnosticMap.set(document.uri.toString(), diagnostics); lastLint.set(document.uri.toString(), (new Date()).getTime()); resetDiagnostics(); -} \ No newline at end of file +} diff --git a/src/linter.ts b/src/linter.ts index 627a025..36616a8 100644 --- a/src/linter.ts +++ b/src/linter.ts @@ -1,4 +1,16 @@ -import { Diagnostic, DiagnosticSeverity, Range, Position } from 'vscode'; +import type { Diagnostic as Diagnostic_vscode } from 'vscode'; +import type { Diagnostic as Diagnostic_coc } from 'coc.nvim'; +type Diagnostic = Diagnostic_vscode | Diagnostic_coc; +let vscode; +try { + vscode = require('vscode'); +} catch (error) { + vscode = require('coc.nvim'); +} +const DiagnosticSeverity = vscode.DiagnosticSeverity; +const Range = vscode.Range; +const Position = vscode.Position; +const Diagnostic = vscode.Diagnostic; import * as WriteGood from 'write-good'; interface Suggestion { @@ -13,9 +25,15 @@ export function lintText(content: string, wgConfig: object, startingLine: number lines.forEach((line, lineCount) => { const suggestions : Suggestion[] = WriteGood(line, wgConfig); suggestions.forEach((suggestion) => { - const start = new Position(lineCount + startingLine, suggestion.index); - const end = new Position(lineCount + startingLine, suggestion.index + suggestion.offset); - diagnostics.push(new Diagnostic(new Range(start, end), suggestion.reason, DiagnosticSeverity.Warning)); + try { + const start = new Position(lineCount + startingLine, suggestion.index); + const end = new Position(lineCount + startingLine, suggestion.index + suggestion.offset); + diagnostics.push(new Diagnostic(new Range(start, end), suggestion.reason, DiagnosticSeverity.Warning)); + } catch (e) { + const start = Position.create(lineCount + startingLine, suggestion.index); + const end = Position.create(lineCount + startingLine, suggestion.index + suggestion.offset); + diagnostics.push(Diagnostic.create(Range.create(start, end), suggestion.reason, DiagnosticSeverity.Warning)); + } }); }); -} \ No newline at end of file +} diff --git a/tsconfig.json b/tsconfig.json index 9375175..1651b50 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "target": "es6", "outDir": "out", "lib": [ "es6", "dom" ], + "allowSyntheticDefaultImports": true, "sourceMap": true, "rootDir": "." }, @@ -11,4 +12,4 @@ "node_modules", ".vscode-test" ] -} \ No newline at end of file +}