All files / src/lsp highlight.ts

85% Statements 34/40
50% Branches 6/12
100% Functions 6/6
85% Lines 34/40

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 72 73 74 75 76 77 78 79 80    1x 1x 1x     1x       4x       2x 2x       2x 2x 2x 1x   2x       1x 1x       1x 1x 1x 1x   1x       1x 1x       1x 1x 1x 1x   1x           4x 4x     4x       4x 4x       4x 4x 4x          
import * as LServer from "vscode-languageserver-types";
import {IRegistry} from "../_iregistry";
import {ABAPObject} from "../objects/_abap_object";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {LSPUtils} from "./_lsp_utils";
import {ISpaghettiScope} from "../abap/5_syntax/_spaghetti_scope";
 
export class Highlight {
  private readonly reg: IRegistry;
 
  public constructor(reg: IRegistry) {
    this.reg = reg;
  }
 
  public listDefinitionPositions(textDocument: LServer.TextDocumentIdentifier): LServer.Range[] {
    const spaghetti = this.runSyntax(textDocument);
    Iif (spaghetti === undefined) {
      return [];
    }
 
    const defs = spaghetti.listDefinitions(textDocument.uri);
    const ret: LServer.Range[] = [];
    for (const d of defs) {
      ret.push(LSPUtils.tokenToRange(d.identifier.getToken()));
    }
    return ret;
  }
 
  public listReadPositions(textDocument: LServer.TextDocumentIdentifier): LServer.Range[] {
    const spaghetti = this.runSyntax(textDocument);
    Iif (spaghetti === undefined) {
      return [];
    }
 
    const reads = spaghetti.listReadPositions(textDocument.uri);
    const ret: LServer.Range[] = [];
    for (const d of reads) {
      ret.push(LSPUtils.tokenToRange(d.getToken()));
    }
    return ret;
  }
 
  public listWritePositions(textDocument: LServer.TextDocumentIdentifier): LServer.Range[] {
    const spaghetti = this.runSyntax(textDocument);
    Iif (spaghetti === undefined) {
      return [];
    }
 
    const writes = spaghetti.listWritePositions(textDocument.uri);
    const ret: LServer.Range[] = [];
    for (const d of writes) {
      ret.push(LSPUtils.tokenToRange(d.getToken()));
    }
    return ret;
  }
 
////////////////////////
 
  private runSyntax(textDocument: LServer.TextDocumentIdentifier): ISpaghettiScope | undefined {
    const obj = this.findObject(textDocument);
    Iif (obj === undefined) {
      return undefined;
    }
    return new SyntaxLogic(this.reg, obj).run().spaghetti;
  }
 
  private findObject(textDocument: LServer.TextDocumentIdentifier): ABAPObject | undefined {
    const file = LSPUtils.getABAPFile(this.reg, textDocument.uri);
    Iif (file === undefined) {
      return undefined;
    }
 
    const obj = this.reg.getObject(file.getObjectType(), file.getObjectName());
    Eif (obj instanceof ABAPObject) {
      return obj;
    } else {
      return undefined;
    }
  }
}