rintenki-lsp-server 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.js CHANGED
@@ -3,6 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const node_js_1 = require("vscode-languageserver/node.js");
4
4
  const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
5
5
  const rintenki_1 = require("rintenki");
6
+ let loadParser;
7
+ try {
8
+ loadParser = require("@rintenki/parser-utils").loadParser;
9
+ }
10
+ catch {
11
+ loadParser = () => undefined;
12
+ }
6
13
  const connection = (0, node_js_1.createConnection)(node_js_1.ProposedFeatures.all);
7
14
  const documents = new node_js_1.TextDocuments(vscode_languageserver_textdocument_1.TextDocument);
8
15
  let config;
@@ -60,12 +67,34 @@ function toLspDiagnostic(d) {
60
67
  data: { fixable: d.fixable },
61
68
  };
62
69
  }
70
+ const SUPPORTED_LANGUAGES = ["html", "vue", "erb", "javascriptreact", "typescriptreact"];
63
71
  function validateDocument(document) {
64
- if (document.languageId !== "html") {
72
+ if (!SUPPORTED_LANGUAGES.includes(document.languageId)) {
65
73
  return;
66
74
  }
67
- const text = document.getText();
75
+ let text = document.getText();
76
+ let lineOffset = 0;
77
+ // Preprocess non-HTML files
78
+ const uri = document.uri;
79
+ const filePath = uri.startsWith("file://") ? decodeURIComponent(uri.slice(7)) : uri;
80
+ const parser = loadParser(filePath);
81
+ if (parser) {
82
+ const preprocessed = parser.preprocess(text);
83
+ text = preprocessed.html;
84
+ lineOffset = preprocessed.lineOffset;
85
+ if (!text) {
86
+ connection.sendDiagnostics({ uri: document.uri, diagnostics: [] });
87
+ return;
88
+ }
89
+ }
68
90
  const result = (0, rintenki_1.lint)(text, config);
91
+ // Remap line numbers
92
+ if (lineOffset > 0) {
93
+ for (const d of result.diagnostics) {
94
+ if (d.line != null)
95
+ d.line += lineOffset;
96
+ }
97
+ }
69
98
  const diagnostics = result.diagnostics.map(toLspDiagnostic);
70
99
  connection.sendDiagnostics({ uri: document.uri, diagnostics });
71
100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rintenki-lsp-server",
3
- "version": "0.0.0",
3
+ "version": "0.2.0",
4
4
  "description": "LSP server for rintenki HTML linter",
5
5
  "author": "Kazuhiro Kobayashi <https://github.com/kzhrk>",
6
6
  "license": "MIT",
@@ -20,10 +20,13 @@
20
20
  "language-server"
21
21
  ],
22
22
  "main": "dist/server.js",
23
+ "files": [
24
+ "dist"
25
+ ],
23
26
  "dependencies": {
24
27
  "vscode-languageserver": "9.0.1",
25
28
  "vscode-languageserver-textdocument": "1.0.12",
26
- "rintenki": "0.0.0"
29
+ "rintenki": "0.2.0"
27
30
  },
28
31
  "devDependencies": {
29
32
  "typescript": "6.0.2"
package/src/server.ts DELETED
@@ -1,138 +0,0 @@
1
- import {
2
- createConnection,
3
- TextDocuments,
4
- ProposedFeatures,
5
- InitializeParams,
6
- InitializeResult,
7
- TextDocumentSyncKind,
8
- Diagnostic,
9
- DiagnosticSeverity,
10
- CodeAction,
11
- CodeActionKind,
12
- TextEdit,
13
- CodeActionParams,
14
- } from "vscode-languageserver/node.js";
15
- import { TextDocument } from "vscode-languageserver-textdocument";
16
- import { lint, fix, type LintConfig, type LintDiagnostic } from "rintenki";
17
-
18
- const connection = createConnection(ProposedFeatures.all);
19
- const documents = new TextDocuments(TextDocument);
20
-
21
- let config: LintConfig | undefined;
22
-
23
- connection.onInitialize((_params: InitializeParams): InitializeResult => {
24
- return {
25
- capabilities: {
26
- textDocumentSync: TextDocumentSyncKind.Full,
27
- codeActionProvider: {
28
- codeActionKinds: [CodeActionKind.QuickFix],
29
- },
30
- },
31
- };
32
- });
33
-
34
- connection.onInitialized(() => {
35
- connection.workspace.getConfiguration("rintenki").then(
36
- (settings) => {
37
- if (settings?.rules) {
38
- config = { rules: settings.rules };
39
- }
40
- },
41
- () => {
42
- // Configuration not available, use defaults
43
- }
44
- );
45
- });
46
-
47
- connection.onDidChangeConfiguration((_change) => {
48
- connection.workspace.getConfiguration("rintenki").then(
49
- (settings) => {
50
- if (settings?.rules) {
51
- config = { rules: settings.rules };
52
- } else {
53
- config = undefined;
54
- }
55
- documents.all().forEach(validateDocument);
56
- },
57
- () => {
58
- // Configuration not available, use defaults
59
- }
60
- );
61
- });
62
-
63
- documents.onDidChangeContent((change) => {
64
- validateDocument(change.document);
65
- });
66
-
67
- function toDiagnosticSeverity(severity: string): DiagnosticSeverity {
68
- return severity === "error"
69
- ? DiagnosticSeverity.Error
70
- : DiagnosticSeverity.Warning;
71
- }
72
-
73
- function toLspDiagnostic(d: LintDiagnostic): Diagnostic {
74
- const line = Math.max((d.line ?? 1) - 1, 0);
75
- return {
76
- range: {
77
- start: { line, character: 0 },
78
- end: { line, character: Number.MAX_SAFE_INTEGER },
79
- },
80
- severity: toDiagnosticSeverity(d.severity),
81
- source: "rintenki",
82
- code: d.rule,
83
- message: d.message,
84
- data: { fixable: d.fixable },
85
- };
86
- }
87
-
88
- function validateDocument(document: TextDocument): void {
89
- if (document.languageId !== "html") {
90
- return;
91
- }
92
-
93
- const text = document.getText();
94
- const result = lint(text, config);
95
- const diagnostics = result.diagnostics.map(toLspDiagnostic);
96
- connection.sendDiagnostics({ uri: document.uri, diagnostics });
97
- }
98
-
99
- connection.onCodeAction((params: CodeActionParams): CodeAction[] => {
100
- const document = documents.get(params.textDocument.uri);
101
- if (!document) return [];
102
-
103
- const fixableDiagnostics = params.context.diagnostics.filter(
104
- (d) => d.source === "rintenki" && (d.data as any)?.fixable
105
- );
106
-
107
- if (fixableDiagnostics.length === 0) return [];
108
-
109
- const text = document.getText();
110
- const fixResult = fix(text, config);
111
-
112
- if (fixResult.fixedCount === 0) return [];
113
-
114
- const lastLine = Math.max(document.lineCount - 1, 0);
115
- return [
116
- {
117
- title: "Fix all auto-fixable rintenki issues",
118
- kind: CodeActionKind.QuickFix,
119
- diagnostics: fixableDiagnostics,
120
- edit: {
121
- changes: {
122
- [params.textDocument.uri]: [
123
- TextEdit.replace(
124
- {
125
- start: { line: 0, character: 0 },
126
- end: { line: lastLine, character: Number.MAX_SAFE_INTEGER },
127
- },
128
- fixResult.output
129
- ),
130
- ],
131
- },
132
- },
133
- },
134
- ];
135
- });
136
-
137
- documents.listen(connection);
138
- connection.listen();
package/tsconfig.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "NodeNext",
5
- "moduleResolution": "NodeNext",
6
- "outDir": "dist",
7
- "rootDir": "src",
8
- "strict": true,
9
- "esModuleInterop": true,
10
- "declaration": true,
11
- "skipLibCheck": true
12
- },
13
- "include": ["src"],
14
- "exclude": ["node_modules", "dist"]
15
- }