-
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
Conversation
Implement a VS Code extension for real-time linting during document editing for Markdown files. * **New File `src/extension.ts`**: - Create a new file to implement the VS Code extension. - Register the extension with the VS Code API. - Add event listeners for document changes to trigger linting. - Integrate with the VS Code Problems panel to display linting errors. - Implement real-time linting for Markdown files using `doc-structure-lint`. * **`package.json`**: - Update the `main` field to point to the new extension entry point. - Add `vscode` as a dependency for the VS Code extension. - Add a new script to build the VS Code extension. * **`README.md`**: - Add instructions for setting up and using the VS Code extension. - Include details about the real-time linting feature.
WalkthroughThe pull request introduces significant enhancements to the documentation and functionality of the Changes
Possibly related PRs
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 4
🧹 Outside diff range and nitpick comments (3)
src/extension.ts (2)
8-13
: Consider debouncing the linting to improve performanceTriggering
lintAndReport
on every text change may impact performance, especially with large documents. Implementing a debounce can limit the frequency of linting operations.Apply this diff to debounce the linting function:
context.subscriptions.push( vscode.workspace.onDidChangeTextDocument((event) => { if (event.document.languageId === 'markdown') { - lintAndReport(event.document, diagnosticCollection); + debouncedLint(event.document, diagnosticCollection); } }) ); +const debouncedLint = debounce((document: vscode.TextDocument, diagnosticCollection: vscode.DiagnosticCollection) => { + lintAndReport(document, diagnosticCollection); +}, 300); +function debounce(func: (...args: any[]) => void, wait: number) { + let timeout: NodeJS.Timeout; + return (...args: any[]) => { + clearTimeout(timeout); + timeout = setTimeout(() => func(...args), wait); + }; +}
53-54
: Ensure proper error message retrieval in the catch blockThe
error
object may not always have amessage
property. UseString(error)
to display the error reliably.Apply this diff to improve error handling:
} catch (error) { - vscode.window.showErrorMessage(`Linting failed: ${error.message}`); + vscode.window.showErrorMessage(`Linting failed: ${String(error)}`); }README.md (1)
276-278
: Adjust code block indentation in the setup instructionsThe code block under step 2 is not properly indented, which may affect Markdown rendering. Ensure the code block is indented to align with the list item.
Apply this diff to fix the indentation:
2. Install dependencies: - ```bash - npm install - ``` + ```bash + npm install + ```
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); | ||
}); |
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.
Add checks for error positions to prevent runtime exceptions
If error.position
or its properties are undefined, accessing them can cause runtime errors. Add checks to ensure these properties exist before using them.
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
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
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); | |
}); | |
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); | |
} | |
}); |
"prepare": "husky", | ||
"build": "tsc -p ./" |
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.
🛠️ Refactor suggestion
Include TypeScript compilation in the 'prepare' script
Ensure the TypeScript code is compiled before publishing by adding the build step to the prepare
script.
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
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
"prepare": "husky", | |
"build": "tsc -p ./" | |
"prepare": "husky && npm run build", | |
"build": "tsc -p ./" |
@@ -5,13 +5,14 @@ | |||
"bin": { | |||
"doc-structure-lint": "src/index.js" | |||
}, | |||
"main": "src/index.js", | |||
"main": "src/extension.ts", |
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:
"bin": {
"doc-structure-lint": "src/index.js"
},
-"main": "src/extension.ts",
+"main": "./out/extension.js",
Committable suggestion skipped: line range outside the PR's diff.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Move 'vscode' to 'devDependencies' and specify the VS Code engine version
The vscode
package should be listed under devDependencies
. Also, add an engines
field to specify the compatible VS Code version.
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"
+},
Committable suggestion skipped: line range outside the PR's diff.
Implement a VS Code extension for real-time linting during document editing for Markdown files.
New File
src/extension.ts
:doc-structure-lint
.package.json
:main
field to point to the new extension entry point.vscode
as a dependency for the VS Code extension.README.md
:Summary by CodeRabbit
New Features
Documentation
Chores