Skip to content

Commit 83ad756

Browse files
v0.1.5: debouncing linting attempts
* Updating to latest version of everything * Removed tests, they aren't really doing anything * Added new configuration for debounce time on change events * Added eslint
1 parent 73c6d5f commit 83ad756

9 files changed

Lines changed: 103 additions & 128 deletions

File tree

.eslintrc.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**@type {import('eslint').Linter.Config} */
2+
// eslint-disable-next-line no-undef
3+
module.exports = {
4+
root: true,
5+
parser: '@typescript-eslint/parser',
6+
plugins: [
7+
'@typescript-eslint',
8+
],
9+
extends: [
10+
'eslint:recommended',
11+
'plugin:@typescript-eslint/recommended',
12+
],
13+
rules: {
14+
'semi': [2, "always"],
15+
'@typescript-eslint/no-unused-vars': 0,
16+
'@typescript-eslint/no-explicit-any': 0,
17+
'@typescript-eslint/explicit-module-boundary-types': 0,
18+
'@typescript-eslint/no-non-null-assertion': 0,
19+
}
20+
};

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
out
22
node_modules
33
write-good-linter-*.vsix
4+
.DS_Store
5+
package-lock.json
6+
test.md

.vscode/tasks.json

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,36 @@
88

99
// A task runner that calls a custom npm script that compiles the extension.
1010
{
11-
"version": "0.1.0",
11+
"version": "2.0.0",
1212

1313
// we want to run npm
1414
"command": "npm",
1515

16-
// the command is a shell script
17-
"isShellCommand": true,
18-
19-
// show the output window only if unrecognized errors occur.
20-
"showOutput": "silent",
21-
2216
// we run the custom script "compile" as defined in package.json
2317
"args": ["run", "compile", "--loglevel", "silent"],
2418

2519
// The tsc compiler is started in watching mode
26-
"isWatching": true,
20+
"isBackground": true,
2721

2822
// use the standard tsc in watch mode problem matcher to find compile problems in the output.
29-
"problemMatcher": "$tsc-watch"
23+
"problemMatcher": "$tsc-watch",
24+
"tasks": [
25+
{
26+
"label": "npm",
27+
"type": "shell",
28+
"command": "npm",
29+
"args": [
30+
"run",
31+
"compile",
32+
"--loglevel",
33+
"silent"
34+
],
35+
"isBackground": true,
36+
"problemMatcher": "$tsc-watch",
37+
"group": {
38+
"_id": "build",
39+
"isDefault": false
40+
}
41+
}
42+
]
3043
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Press F1 or CTRL+P (or CMD+P) and type out `> ext install travisthetechie.write-
1414

1515
`write-good.only-lint-on-save` disables linting during editing for large files. A save triggers linting.
1616

17+
`write-good.debounce-time-in-ms` is the minimum time, in milliseconds, that must wait between linting attempts. Saving ignores the minimum time. Default is 200ms. This is useful if linting causes any performance hit and you want to limit it.
18+
1719
## License and acknowledgements
1820

1921
This is licensed under the MIT open source license. Do what you want with this software, just include notice that it orginated with me.

package.json

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,32 @@
22
"name": "write-good-linter",
33
"displayName": "Write Good Linter",
44
"description": "Applies the Write Good Linter to your Markdown, so you can write more good.",
5-
"version": "0.1.4",
5+
"version": "0.1.5",
66
"publisher": "travisthetechie",
77
"engines": {
8-
"vscode": "^1.26.0"
8+
"vscode": "^1.89.0"
99
},
1010
"categories": [
1111
"Linters"
1212
],
1313
"activationEvents": [
14-
"*"
14+
"onStartupFinished"
1515
],
1616
"main": "./out/src/extension",
1717
"contributes": {
1818
"configuration": {
1919
"type": "object",
2020
"title": "write-good",
2121
"properties": {
22+
"write-good.debounce-time-in-ms": {
23+
"type": [
24+
"null",
25+
"number"
26+
],
27+
"default": 200,
28+
"markdownDescription": "Minimum milliseconds between linting attempts. Default is 200ms, increasing this can reduce load when working with large files. Linting on save and load are not rate limited.",
29+
"scope": "resouce"
30+
},
2231
"write-good.only-lint-on-save": {
2332
"type": [
2433
"null",
@@ -56,19 +65,21 @@
5665
}
5766
},
5867
"scripts": {
59-
"vscode:prepublish": "tsc -p ./",
60-
"compile": "tsc -p ./",
61-
"postinstall": "node ./node_modules/vscode/bin/install"
68+
"vscode:prepublish": "npm run compile",
69+
"compile": "tsc -p ./",
70+
"lint": "eslint \"src/**/*.ts\"",
71+
"watch": "tsc -watch -p ./"
6272
},
6373
"devDependencies": {
64-
"@types/mocha": "^5.2.7",
65-
"@types/node": "^10.12.18",
66-
"mocha": "^6.2.2",
67-
"typescript": "^3.7.4",
68-
"vscode": "^1.1.36"
74+
"@typescript-eslint/eslint-plugin": "^6.7.0",
75+
"@typescript-eslint/parser": "^6.7.0",
76+
"eslint": "^8.56.0",
77+
"@types/node": "^20.12.12",
78+
"@types/vscode": "1.89.0",
79+
"typescript": "^5.4.5"
6980
},
7081
"dependencies": {
71-
"write-good": "^1.0.2"
82+
"write-good": "^1.0.8"
7283
},
7384
"license": "MIT",
7485
"repository": {

src/extension.ts

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
'use strict';
22

33
import { workspace, ExtensionContext, TextDocument, languages, Uri,
4-
Diagnostic, DiagnosticCollection, TextDocumentContentChangeEvent } from 'vscode';
5-
import { isNullOrUndefined } from 'util';
4+
Diagnostic, DiagnosticCollection } from 'vscode';
65
import { lintText } from './linter';
76

87
let diagnosticCollection: DiagnosticCollection;
98
let diagnosticMap: Map<string, Diagnostic[]>;
9+
let lastLint: Map<string, number>;
1010

1111
export function activate(context: ExtensionContext) {
1212

1313
console.log("Write-Good Linter active...");
1414
diagnosticCollection = languages.createDiagnosticCollection("Write-Good Lints");
1515
diagnosticMap = new Map();
16+
lastLint = new Map();
1617

1718
if (context == null) return;
1819

1920
function isWriteGoodLanguage(languageId: string) {
20-
let wgLanguages: string = workspace.getConfiguration('write-good').get('languages');
21+
const wgLanguages: string = workspace.getConfiguration('write-good').get('languages');
2122
return (wgLanguages.indexOf(languageId) > -1 || wgLanguages === '*');
2223
}
2324

@@ -30,13 +31,25 @@ export function activate(context: ExtensionContext) {
3031

3132
// attempt to only lint changes on motification
3233
context.subscriptions.push(workspace.onDidChangeTextDocument(event => {
33-
if (isWriteGoodLanguage(event.document.languageId)) {
34-
// this doesn't work yet, instead, check for a setting?
35-
// doPartialLint(event.document, event.contentChanges);
36-
const onlyLintOnSave: Boolean = workspace.getConfiguration('write-good').get('only-lint-on-save');
37-
if (!onlyLintOnSave) {
38-
doLint(event.document);
39-
}
34+
if (!isWriteGoodLanguage(event.document.languageId)) {
35+
// language is unsupported.
36+
return;
37+
}
38+
39+
const onlyLintOnSave: boolean = workspace.getConfiguration('write-good').get('only-lint-on-save');
40+
if (onlyLintOnSave) {
41+
// not a save event, so don't bother linting
42+
return;
43+
}
44+
45+
// debounce linting on editing
46+
const debounceMs: number = workspace.getConfiguration('write-good').get('debounce-time-in-ms');
47+
const lastLintMs: number = lastLint.get(event.document.uri.toString()) || 0;
48+
const nowMs = (new Date()).getTime();
49+
if (lastLintMs + debounceMs < nowMs) {
50+
// debounce time is less than now, let's do this
51+
console.log("LINTING!!!!");
52+
doLint(event.document);
4053
}
4154
}));
4255

@@ -57,7 +70,7 @@ export function activate(context: ExtensionContext) {
5770
}
5871

5972
export function deactivate() {
60-
console.log("Write-Good Linter deactivating...")
73+
console.log("Write-Good Linter deactivating...");
6174
}
6275

6376
function resetDiagnostics() {
@@ -69,41 +82,19 @@ function resetDiagnostics() {
6982
}
7083

7184
function getWriteGoodConfig() : object {
72-
var wgConfig: object = workspace.getConfiguration('write-good').get('write-good-config');
73-
if (isNullOrUndefined(wgConfig)) {
85+
let wgConfig: object = workspace.getConfiguration('write-good').get('write-good-config');
86+
if (wgConfig === undefined || wgConfig === null) {
7487
wgConfig = {};
7588
}
7689
return wgConfig;
7790
}
7891

79-
function doPartialLint(document: TextDocument, changes: TextDocumentContentChangeEvent[]) {
80-
const wgConfig = getWriteGoodConfig();
81-
let diagnostics: Diagnostic[] = diagnosticMap.get(document.uri.toString());
82-
changes.forEach((changeEvent, index) => {
83-
// remove any overlapping diagnostics
84-
let toRemove: number[] = [];
85-
diagnostics.forEach((diagnostic, index) => {
86-
// if the diagnostic range intersects with the change event, we assume the diagnostic should be removed
87-
// and will be re-added with the change event.
88-
if (diagnostic.range.intersection(changeEvent.range) != undefined) {
89-
toRemove.push(index);
90-
}
91-
});
92-
toRemove.forEach((i) => diagnostics.splice(i, 1));
93-
94-
// skip linting if there's content
95-
if(changeEvent.text.length > 0) {
96-
const lineOffset = changeEvent.range.start.line;
97-
lintText(changeEvent.text, wgConfig, lineOffset, diagnostics);
98-
}
99-
});
100-
}
101-
10292
function doLint(document: TextDocument) {
10393
const wgConfig = getWriteGoodConfig();
104-
let diagnostics: Diagnostic[] = [];
94+
const diagnostics: Diagnostic[] = [];
10595
lintText(document.getText(), wgConfig, 0, diagnostics);
10696

10797
diagnosticMap.set(document.uri.toString(), diagnostics);
98+
lastLint.set(document.uri.toString(), (new Date()).getTime());
10899
resetDiagnostics();
109100
}

src/linter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ interface Suggestion {
99

1010
export function lintText(content: string, wgConfig: object, startingLine: number = 0, diagnostics: Diagnostic[] = []) {
1111
if (content == null) return;
12-
let lines = content.split(/\r?\n/g);
12+
const lines = content.split(/\r?\n/g);
1313
lines.forEach((line, lineCount) => {
14-
let suggestions : Suggestion[] = WriteGood(line, wgConfig);
15-
suggestions.forEach((suggestion, si) => {
16-
let start = new Position(lineCount + startingLine, suggestion.index);
17-
let end = new Position(lineCount + startingLine, suggestion.index + suggestion.offset);
14+
const suggestions : Suggestion[] = WriteGood(line, wgConfig);
15+
suggestions.forEach((suggestion) => {
16+
const start = new Position(lineCount + startingLine, suggestion.index);
17+
const end = new Position(lineCount + startingLine, suggestion.index + suggestion.offset);
1818
diagnostics.push(new Diagnostic(new Range(start, end), suggestion.reason, DiagnosticSeverity.Warning));
1919
});
2020
});

test/index.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/linter.test.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)