8000 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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,38 @@ Contributions are welcome! Please feel free to submit a Pull Request.
## License

MIT License - see the [LICENSE](LICENSE) file for details.

## VS Code Extension

### Setup

1. Clone the repository
2. Install dependencies:

```bash
npm install
```

3. Open the repository in VS Code
4. Press `F5` to open a new VS Code window with the extension loaded

### Usage

- The extension provides real-time linting during document editing for Markdown files.
- Linting errors are displayed in the VS Code Problems panel.
- You can trigger linting manually via the command palette by running the `Doc Structure Lint: Lint Document` command.

### Configuration

- You can configure the template source URI in the VS Code settings.
- The extension supports both `https://` and `file://` schemes for the template source URI.
- If the scheme is omitted, it defaults to `file://`.
- You can specify the template at the document level using frontmatter.
- Required: `template` field specifies the template ID.
- Optional: `template_source` field overrides the global template source.

### Real-time Linting

- The extension provides real-time linting during document editing for Markdown files.
- Linting errors are displayed in the VS Code Problems panel as you type.
- The extension uses the `doc-structure-lint` tool to validate the document structure against the specified template.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.

"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
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 ./"

},
"author": "Manny Silva",
"license": "MIT",
Expand All @@ -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.

},
"devDependencies": {
"chai": "^5.1.2",
Expand Down
59 changes: 59 additions & 0 deletions src/extension.ts
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
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);
}
});


diagnosticCollection.set(document.uri, diagnostics);
} catch (error) {
vscode.window.showErrorMessage(`Linting failed: ${error.message}`);
}
}

export function deactivate() {
// Clean up resources if needed
}
Loading
0