10000 Add VS Code extension for real-time linting by hawkeyexl · Pull Request #10 · hawkeyexl/doc-structure-lint · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

hawkeyexl
Copy link
Owner
@hawkeyexl hawkeyexl commented Dec 7, 2024

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.

Summary by CodeRabbit

  • New Features

    • Introduced a Visual Studio Code extension for real-time linting of Markdown documents.
    • Added a "VS Code Extension" section in the README with setup, usage, and configuration details.
  • Documentation

    • Enhanced README with comprehensive guidance on the VS Code extension integration.
  • Chores

    • Updated package.json to reflect changes in the main entry point and added TypeScript compilation to the build process.

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.
Copy link
coderabbitai bot commented Dec 7, 2024

Walkthrough

The pull request introduces significant enhancements to the documentation and functionality of the doc-structure-lint tool. A new section in the README.md details the integration of a Visual Studio Code extension, providing setup instructions, usage guidelines, and configuration options. Additionally, the package.json file updates the main entry point to a TypeScript file and adds a new build script and dependency. A new src/extension.ts file implements the VS Code extension, enabling real-time linting for Markdown documents and providing functions for linting and reporting errors.

Changes

File Change Summary
README.md Added a "VS Code Extension" section with setup instructions, usage details, configuration options, and real-time linting capabilities.
package.json Updated main entry point to "src/extension.ts", added build script "build": "tsc -p ./", and included "vscode": "^1.1.37" dependency.
src/extension.ts Introduced a new VS Code extension for linting Markdown documents, including functions for activation, deactivation, and linting/reporting.

Possibly related PRs

  • Enhance instruction validation and model management #8: The changes in the main PR regarding the integration of a VS Code extension for the doc-structure-lint tool relate to the enhancements in instruction validation and model management, particularly in the context of improving user guidance and documentation, which aligns with the updates made to the README.md file.

🐰 In the garden of code, I hop and play,
With VS Code extensions brightening the way.
Linting documents with joy, oh what a treat,
Real-time feedback makes my work so neat!
So gather 'round, friends, let's code and cheer,
For the magic of linting is finally here! 🌼✨


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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
@coderabbitai coderabbitai bot left a 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 performance

Triggering 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 block

The error object may not always have a message property. Use String(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 instructions

The 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
+    ```
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b3d507a and 6663125.

📒 Files selected for processing (3)
  • README.md (1 hunks)
  • package.json (2 hunks)
  • src/extension.ts (1 hunks)

Comment on lines +42 to +49
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);
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

Suggested change
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);
}
});

Comment on lines +14 to +15
"prepare": "husky",
"build": "tsc -p ./"
Copy link

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.

Suggested change
"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",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

@hawkeyexl hawkeyexl closed this Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0