All files / src/lsp diagnostics.ts

93.05% Statements 67/72
69.23% Branches 9/13
100% Functions 5/5
93.05% Lines 67/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 5x 5x 5x 5x 1x 1x 4x 4x 5x     4x 4x 4x 4x 4x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 5x 5x 5x 5x   5x   5x   5x 5x 1x 1x
import * as LServer from "vscode-languageserver-types";
import {IRegistry} from "../_iregistry";
import {LSPUtils} from "./_lsp_utils";
import {Issue} from "../issue";
import {Severity} from "../severity";
 
export class Diagnostics {
  private readonly reg: IRegistry;
 
  public constructor(reg: IRegistry) {
    this.reg = reg;
  }
 
  public findIssues(textDocument: LServer.TextDocumentIdentifier): readonly Issue[] {
    this.reg.parse();
 
    const file = LSPUtils.getABAPFile(this.reg, textDocument.uri); // todo, this sould also run for xml files
    if (file === undefined) {
      return [];
    }
 
    const obj = this.reg.findObjectForFile(file);
    if (obj === undefined) {
      return [];
    }
 
    let issues = this.reg.findIssuesObject(obj);
    issues = issues.filter(i => i.getFilename() === file.getFilename());
    return issues;
  }
 
  public static mapDiagnostic(issue: Issue): LServer.Diagnostic {
    const diagnosic: LServer.Diagnostic = {
      severity: this.mapSeverity(issue.getSeverity()),
      range: {
        start: {line: issue.getStart().getRow() - 1, character: issue.getStart().getCol() - 1},
        end: {line: issue.getEnd().getRow() - 1, character: issue.getEnd().getCol() - 1},
      },
      code: issue.getKey(),
      codeDescription: {href: "https://rules.abaplint.org/" + issue.getKey() + "/"},
      message: issue.getMessage().toString(),
      source: "abaplint",
    };
 
    return diagnosic;
  }
 
  public find(textDocument: LServer.TextDocumentIdentifier): LServer.Diagnostic[] {
    const issues = this.findIssues(textDocument);
 
    const diagnostics: LServer.Diagnostic[] = [];
    for (const issue of issues) {
      diagnostics.push(Diagnostics.mapDiagnostic(issue));
    }
 
    return diagnostics;
  }
 
  private static mapSeverity(severity: Severity): LServer.DiagnosticSeverity {
    switch (severity) {
      case Severity.Error:
        return LServer.DiagnosticSeverity.Error;
      case Severity.Warning:
        return LServer.DiagnosticSeverity.Warning;
      case Severity.Info:
        return LServer.DiagnosticSeverity.Information;
      default:
        return LServer.DiagnosticSeverity.Error;
    }
  }
 
}