Skip to content

Commit 92faf26

Browse files
committed
provide better ranges and fix messages
1 parent 0a17de8 commit 92faf26

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

Diff for: src/diagnostic.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CodeAction, CodeActionKind, Diagnostic, DiagnosticSeverity, DocumentDiagnosticReportKind, FullDocumentDiagnosticReport, Range } from 'lsp-types';
2-
import { FunctionStatement, GlobalStatement, ImportStatement, KeywordStatement, NodeType, OperatorStatement, ProgramStatement, RuleStatement, Statement, TokenType, isCompilerError, statementIsA } from './types.js';
2+
import { FunctionStatement, GlobalStatement, IdentifierExpression, ImportStatement, KeywordStatement, NodeType, OperatorStatement, ProgramStatement, RuleStatement, Statement, TokenType, isCompilerError, statementIsA } from './types.js';
33
import { existsSync, readFileSync, statSync } from 'fs';
44
import { sysparser, syxparser } from './ast.js';
55
import { tokenizeSys, tokenizeSyx } from './lexer.js';
@@ -65,12 +65,12 @@ function ruleConflictCheck(ast: ProgramStatement, filePath: string): Diagnostic[
6565

6666
ast.body.filter(r => statementIsA(r, NodeType.Rule)).filter(r => r.range !== stmt.range).map(r => r as RuleStatement).forEach(otherRules => {
6767
if (dictRule.conflicts.includes(otherRules.rule.value)) items.push({
68-
message: `Rule '${otherRules.rule}' conflicts with '${stmt.rule}', Both of them should not be defined.`,
69-
range: subRange(otherRules.range),
68+
message: `Rule '${otherRules.rule.value}' conflicts with '${stmt.rule.value}', Both of them should not be defined.`,
69+
range: subRange(otherRules.rule.range),
7070
severity: DiagnosticSeverity.Warning,
7171
data: [
7272
{
73-
title: `Remove ${stmt.rule} definition`,
73+
title: `Remove ${stmt.rule.value} definition`,
7474
kind: CodeActionKind.QuickFix,
7575
edit: {
7676
changes: {
@@ -84,7 +84,7 @@ function ruleConflictCheck(ast: ProgramStatement, filePath: string): Diagnostic[
8484
}
8585
},
8686
{
87-
title: `Remove ${otherRules.rule} definition`,
87+
title: `Remove ${otherRules.rule.value} definition`,
8888
kind: CodeActionKind.QuickFix,
8989
edit: {
9090
changes: {
@@ -114,8 +114,8 @@ function sameRuleCheck(ast: ProgramStatement, filePath: string): Diagnostic[] {
114114
if (statementIsA(stmt, NodeType.Rule)) {
115115
ast.body.filter(r => statementIsA(r, NodeType.Rule)).filter(r => r.range !== stmt.range).map(r => r as RuleStatement).forEach(otherRules => {
116116
if (otherRules.rule === stmt.rule) items.push({
117-
message: `Rule '${stmt.rule}' is already defined.`,
118-
range: subRange(stmt.range),
117+
message: `Rule '${stmt.rule.value}' is already defined.`,
118+
range: subRange(stmt.rule.range),
119119
severity: DiagnosticSeverity.Error,
120120
data: [
121121
{
@@ -152,7 +152,7 @@ function importedExistentCheck(ast: ProgramStatement, filePath: string): Diagnos
152152
if (!existsSync(fullPath)) items.push({
153153
message: `Can't find file '${fullPath}' imported from '${filePathButPath}'`,
154154
severity: DiagnosticSeverity.Error,
155-
range: subRange(stmt.range),
155+
range: subRange(stmt.path.range),
156156
data: [
157157
{
158158
title: 'Remove this import statement',
@@ -174,7 +174,7 @@ function importedExistentCheck(ast: ProgramStatement, filePath: string): Diagnos
174174
if (!status.isFile()) items.push({
175175
message: `'${fullPath}' imported from '${filePathButPath}' doesn't seem to be a file.`,
176176
severity: DiagnosticSeverity.Error,
177-
range: subRange(stmt.range),
177+
range: subRange(stmt.path.range),
178178
data: [
179179
{
180180
title: 'Remove this import statement',
@@ -193,7 +193,7 @@ function importedExistentCheck(ast: ProgramStatement, filePath: string): Diagnos
193193
if (!fullPath.endsWith('.syx')) items.push({
194194
message: `'${fullPath}' imported from '${filePathButPath}' cannot be imported.`,
195195
severity: DiagnosticSeverity.Error,
196-
range: subRange(stmt.range),
196+
range: subRange(stmt.path.range),
197197
data: [
198198
{
199199
title: 'Remove this import statement',
@@ -262,7 +262,7 @@ function exportableCheck(statements: Statement[], filePath: string): Diagnostic[
262262

263263
if (stmt.modifiers.some(t => t.type === TokenType.ExportKeyword) && !dictionary.ExportableNodeTypes.includes(stmt.type)) items.push({
264264
message: 'This statement cannot be exported.',
265-
range: subRange(stmt.range),
265+
range: subRange(stmt.modifiers.find(r=>r.type===TokenType.ExportKeyword).range),
266266
severity: DiagnosticSeverity.Error,
267267
data: [
268268
{
@@ -302,14 +302,14 @@ function sameNameCheck(statements: Statement[], filePath: string): Diagnostic[]
302302
if (statementIsA(r, NodeType.Keyword)) return r as KeywordStatement;
303303
}).forEach(stmt => {
304304

305-
const n = stmt[statementIsA(stmt, NodeType.Keyword) ? 'word' : 'name'];
306-
307-
if (encounteredNames.includes(n)) items.push({
308-
message: `Name '${n}' is already seen before.`,
309-
range: subRange(stmt.range),
305+
const n:IdentifierExpression = stmt[statementIsA(stmt, NodeType.Keyword) ? 'word' : 'name'];
306+
307+
if (encounteredNames.includes(n.value)) items.push({
308+
message: `Name '${n.value}' is already seen before.`,
309+
range: subRange(n.range),
310310
severity: DiagnosticSeverity.Error
311311
});
312-
else encounteredNames.push(n);
312+
else encounteredNames.push(n.value);
313313

314314
if (statementIsA(stmt, NodeType.Global)) c(stmt.body);
315315
});

0 commit comments

Comments
 (0)