Skip to content

Commit

Permalink
Moves ASR/LFortran->LSP type mapping logic to LFortran
Browse files Browse the repository at this point in the history
  • Loading branch information
dylon committed Dec 5, 2024
1 parent 777b869 commit 50fdec8
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 112 deletions.
74 changes: 0 additions & 74 deletions server/src/lfortran-accessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import {

import {
Diagnostic,
DiagnosticSeverity,
Location,
Position,
Range,
SymbolInformation,
SymbolKind,
TextEdit,
} from 'vscode-languageserver/node';

import {
ASRSymbolType,
ExampleSettings,
ErrorDiagnostics,
LFortranDiagnosticLevel,
} from './lfortran-types';

import which from 'which';
Expand Down Expand Up @@ -67,72 +63,6 @@ export interface LFortranAccessor {
settings: ExampleSettings): Promise<TextEdit[]>;
}

const asr_symbolType_to_lsp_SymbolKind: Map<ASRSymbolType, SymbolKind | null> = new Map([
[ASRSymbolType.Program, null],
[ASRSymbolType.Module, SymbolKind.Module],
[ASRSymbolType.Function, SymbolKind.Function],
[ASRSymbolType.GenericProcedure, SymbolKind.Function],
[ASRSymbolType.CustomOperator, SymbolKind.Operator],
[ASRSymbolType.ExternalSymbol, null],
[ASRSymbolType.Struct, SymbolKind.Struct],
[ASRSymbolType.Enum, SymbolKind.Enum],
[ASRSymbolType.UnionType, null],
[ASRSymbolType.Variable, SymbolKind.Variable],
[ASRSymbolType.Class, SymbolKind.Class],
[ASRSymbolType.ClassProcedure, SymbolKind.Method],
[ASRSymbolType.AssociateBlock, null],
[ASRSymbolType.Block, null],
[ASRSymbolType.Requirement, null],
[ASRSymbolType.Template, SymbolKind.TypeParameter],
]);

function getLspSymbolKindFromAsrSymbolType(symbolType: number): SymbolKind {
const symbolKind: SymbolKind | null | undefined =
asr_symbolType_to_lsp_SymbolKind.get(symbolType);

if (symbolKind === undefined) {
console.warn("Unrecognized ASR::symbolType; cannot map to LSP SymbolKind: %d", symbolType);
return SymbolKind.Function;
}

if (symbolKind === null) {
console.debug("No direct mapping of ASR::symbolType to LSP SymbolKind: %d", symbolType);
return SymbolKind.Function;
}

return symbolKind;
}

const lfortran_diagnostic_level_to_lsp_DiagnosticSeverity: Map<LFortranDiagnosticLevel, DiagnosticSeverity | null> = new Map([
[LFortranDiagnosticLevel.Error, DiagnosticSeverity.Error],
[LFortranDiagnosticLevel.Warning, DiagnosticSeverity.Warning],
[LFortranDiagnosticLevel.Note, DiagnosticSeverity.Information],
[LFortranDiagnosticLevel.Help, DiagnosticSeverity.Hint],
[LFortranDiagnosticLevel.Style, DiagnosticSeverity.Warning],
]);

function getLspDiagnosticSeverityFromLFortranDiagnosticLevel(level: number | undefined): DiagnosticSeverity {
if (level === undefined) {
console.warn("Cannot map `undefined` to LSP DiagnosticSeverity");
return DiagnosticSeverity.Warning;
}

const severity: DiagnosticSeverity | null | undefined =
lfortran_diagnostic_level_to_lsp_DiagnosticSeverity.get(level);

if (severity === undefined) {
console.warn("Unrecognized diag::Level; cannot map to LSP DiagnosticSeverity: %d", level);
return DiagnosticSeverity.Warning;
}

if (severity === null) {
console.debug("No direct mapping of diag::Level to LSP DiagnosticSeverity: %d", level);
return DiagnosticSeverity.Warning;
}

return severity;
}

/**
* Interacts with LFortran through its command-line interface.
*/
Expand Down Expand Up @@ -275,8 +205,6 @@ export class LFortranCLIAccessor implements LFortranAccessor {

const end: Position = range.end;
end.character--;

symbol.kind = getLspSymbolKindFromAsrSymbolType(symbol.kind);
}
return symbols;
}
Expand Down Expand Up @@ -353,8 +281,6 @@ export class LFortranCLIAccessor implements LFortranAccessor {
const k = Math.min(results.diagnostics.length, settings.maxNumberOfProblems);
for (let i = 0; i < k; i++) {
const diagnostic: Diagnostic = results.diagnostics[i];
diagnostic.severity =
getLspDiagnosticSeverityFromLFortranDiagnosticLevel(diagnostic.severity);
diagnostic.source = "lfortran-lsp";
diagnostic.range.start.character--;
diagnostics.push(diagnostic);
Expand Down
31 changes: 0 additions & 31 deletions server/src/lfortran-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,3 @@ export interface ExampleSettings {
export interface ErrorDiagnostics {
diagnostics: Diagnostic[];
}

export namespace ASRSymbolType {
export const Program: number = 0;
export const Module: number = 1;
export const Function: number = 2;
export const GenericProcedure: number = 3;
export const CustomOperator: number = 4;
export const ExternalSymbol: number = 5;
export const Struct: number = 6;
export const Enum: number = 7;
export const UnionType: number = 8;
export const Variable: number = 9;
export const Class: number = 10;
export const ClassProcedure: number = 11;
export const AssociateBlock: number = 12;
export const Block: number = 13;
export const Requirement: number = 14;
export const Template: number = 15;
}

export type ASRSymbolType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15;

export namespace LFortranDiagnosticLevel {
export const Error: number = 0;
export const Warning: number = 1;
export const Note: number = 2;
export const Help: number = 3;
export const Style: number = 4;
}

export type LFortranDiagnosticLevel = 0 | 1 | 2 | 3 | 4;
6 changes: 2 additions & 4 deletions server/test/spec/lfortran-accessors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import { LFortranCLIAccessor } from "../../src/lfortran-accessors";

import { settings } from "./lfortran-common";

import { LFortranDiagnosticLevel } from '../../src/lfortran-types';

import { assert } from "chai";

import "mocha";
Expand Down Expand Up @@ -258,7 +256,7 @@ describe("LFortranCLIAccessor", () => {
character: 20
}
},
severity: LFortranDiagnosticLevel.Warning,
severity: DiagnosticSeverity.Warning,
source: "lfortran-lsp",
message: "foo should be bar"
},
Expand All @@ -273,7 +271,7 @@ describe("LFortranCLIAccessor", () => {
character: 17
}
},
severity: LFortranDiagnosticLevel.Warning,
severity: DiagnosticSeverity.Warning,
source: "lfortran-lsp",
message: "baz should be qux"
},
Expand Down
5 changes: 2 additions & 3 deletions server/test/spec/lfortran-language-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { TextDocument } from "vscode-languageserver-textdocument";

import {
ExampleSettings,
LFortranDiagnosticLevel,
} from '../../src/lfortran-types';

import { LFortranCLIAccessor } from "../../src/lfortran-accessors";
Expand Down Expand Up @@ -332,7 +331,7 @@ describe("LFortranLanguageServer", () => {
character: 20
}
},
severity: LFortranDiagnosticLevel.Warning,
severity: DiagnosticSeverity.Warning,
source: "lfortran-lsp",
message: "foo should be bar"
},
Expand All @@ -348,7 +347,7 @@ describe("LFortranLanguageServer", () => {
}
},
// NOTE: Right now, the severity is hard-coded to Warning ...
severity: LFortranDiagnosticLevel.Warning,
severity: DiagnosticSeverity.Warning,
source: "lfortran-lsp",
message: "baz should be qux"
},
Expand Down

0 comments on commit 50fdec8

Please sign in to comment.