-
Notifications
You must be signed in to change notification settings - Fork 2
Add VS Code extension for real-time linting #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,13 +5,14 @@ | |||||||||
"bin": { | ||||||||||
"doc-structure-lint": "src/index.js" | ||||||||||
}, | ||||||||||
"main": "src/index.js", | ||||||||||
"main": "src/extension.ts", | ||||||||||
"scripts": { | ||||||||||
"postinstall": "node src/util/preloadModel.js", | ||||||||||
"clean": "node src/util/tempDir.js clean", | ||||||||||
"start": "node src/index.js", | ||||||||||
"test": "mocha 'src/**/*.test.js'", | ||||||||||
"prepare": "husky" | ||||||||||
"prepare": "husky", | ||||||||||
"build": "tsc -p ./" | ||||||||||
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Include TypeScript compilation in the 'prepare' script Ensure the TypeScript code is compiled before publishing by adding the build step to the Apply this diff to modify the scripts: "scripts": {
"postinstall": "node src/util/preloadModel.js",
"clean": "node src/util/tempDir.js clean",
"start": "node src/index.js",
"test": "mocha 'src/**/*.test.js'",
- "prepare": "husky",
+ "prepare": "husky && npm run build",
"build": "tsc -p ./"
}, 📝 Committable suggestion
Suggested change
|
||||||||||
}, | ||||||||||
"author": "Manny Silva", | ||||||||||
"license": "MIT", | ||||||||||
|
@@ -25,7 +26,8 @@ | |||||||||
"remark-frontmatter": "^5.0.0", | ||||||||||
"uuid": "^11.0.3", | ||||||||||
"yaml": "^2.6.1", | ||||||||||
"yargs": "^17.7.2" | ||||||||||
"yargs": "^17.7.2", | ||||||||||
"vscode": "^1.1.37" | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move 'vscode' to 'devDependencies' and specify the VS Code engine version The Apply this diff to adjust dependencies and specify the engine: "yargs": "^17.7.2",
- "vscode": "^1.1.37"
},
+"devDependencies": {
+ "vscode": "^1.1.37",
+ // existing devDependencies
+},
+"engines": {
+ "vscode": "^1.60.0"
+},
|
||||||||||
}, | ||||||||||
"devDependencies": { | ||||||||||
"chai": "^5.1.2", | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
import * as vscode from 'vscode'; | ||||||||||||||||||||||||||||||||||||||||||||||
import { lintDocument } from './index'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
export function activate(context: vscode.ExtensionContext) { | ||||||||||||||||||||||||||||||||||||||||||||||
const diagnosticCollection = vscode.languages.createDiagnosticCollection('doc-structure-lint'); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
context.subscriptions.push( | ||||||||||||||||||||||||||||||||||||||||||||||
vscode.workspace.onDidChangeTextDocument((event) => { | ||||||||||||||||||||||||||||||||||||||||||||||
if (event.document.languageId === 'markdown') { | ||||||||||||||||||||||||||||||||||||||||||||||
lintAndReport(event.document, diagnosticCollection); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
context.subscriptions.push( | ||||||||||||||||||||||||||||||||||||||||||||||
vscode.workspace.onDidSaveTextDocument((document) => { | ||||||||||||||||||||||||||||||||||||||||||||||
if (document.languageId === 'markdown') { | ||||||||||||||||||||||||||||||||||||||||||||||
lintAndReport(document, diagnosticCollection); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
context.subscriptions.push( | ||||||||||||||||||||||||||||||||||||||||||||||
vscode.commands.registerCommand('doc-structure-lint.lintDocument', () => { | ||||||||||||||||||||||||||||||||||||||||||||||
const editor = vscode.window.activeTextEditor; | ||||||||||||||||||||||||||||||||||||||||||||||
if (editor && editor.document.languageId === 'markdown') { | ||||||||||||||||||||||||||||||||||||||||||||||
lintAndReport(editor.document, diagnosticCollection); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
async function lintAndReport(document: vscode.TextDocument, diagnosticCollection: vscode.DiagnosticCollection) { | ||||||||||||||||||||||||||||||||||||||||||||||
const filePath = document.uri.fsPath; | ||||||||||||||||||||||||||||||||||||||||||||||
const templatePath = vscode.workspace.getConfiguration('doc-structure-lint').get('templatePath', './templates.yaml'); | ||||||||||||||||||||||||||||||||||||||||||||||
const template = vscode.workspace.getConfiguration('doc-structure-lint').get('template', 'default'); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||
const result = await lintDocument({ file: filePath, templatePath, template }); | ||||||||||||||||||||||||||||||||||||||||||||||
const diagnostics: vscode.Diagnostic[] = []; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
result.errors.forEach((error) => { | ||||||||||||||||||||||||||||||||||||||||||||||
const range = new vscode.Range( | ||||||||||||||||||||||||||||||||||||||||||||||
document.positionAt(error.position.start.offset), | ||||||||||||||||||||||||||||||||||||||||||||||
document.positionAt(error.position.end.offset) | ||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||
const diagnostic = new vscode.Diagnostic(range, error.message, vscode.DiagnosticSeverity.Error); | ||||||||||||||||||||||||||||||||||||||||||||||
diagnostics.push(diagnostic); | ||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+42
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add checks for error positions to prevent runtime exceptions If Apply this diff to add safeguards: result.errors.forEach((error) => {
+ if (error.position && error.position.start && error.position.end) {
const range = new vscode.Range(
document.positionAt(error.position.start.offset),
document.positionAt(error.position.end.offset)
);
const diagnostic = new vscode.Diagnostic(range, error.message, vscode.DiagnosticSeverity.Error);
diagnostics.push(diagnostic);
+ } else {
+ // Handle errors without position information
+ const diagnostic = new vscode.Diagnostic(new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)), error.message, vscode.DiagnosticSeverity.Error);
+ diagnostics.push(diagnostic);
+ }
}); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
diagnosticCollection.set(document.uri, diagnostics); | ||||||||||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||
vscode.window.showErrorMessage(`Linting failed: ${error.message}`); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
export function deactivate() { | ||||||||||||||||||||||||||||||||||||||||||||||
// Clean up resources if needed | ||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the 'main' field to point to the compiled JavaScript file
The
main
field should reference the compiled JavaScript file (out/extension.js
), not the TypeScript source file.Apply this diff to correct the entry point: