All files / src/lsp symbols.ts

92.86% Statements 104/112
77.78% Branches 14/18
100% Functions 10/10
92.86% Lines 104/112

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 1121x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 5x 5x     5x 5x 5x 5x 5x 5x 1x 1x 6x 6x 6x 6x 1x 1x 6x 6x 6x 6x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 5x 5x 1x 1x 1x 5x 5x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 5x 5x 3x 3x 3x 3x 3x 5x 5x 5x 1x 1x 3x 3x 1x 1x 1x 3x 3x 1x 1x 1x     1x 1x 1x     1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x
import * as LServer from "vscode-languageserver-types";
import {IRegistry} from "../_iregistry";
import {Identifier} from "../abap/4_file_information/_identifier";
import {LSPUtils} from "./_lsp_utils";
import {InfoAttribute, InfoMethodDefinition} from "../abap/4_file_information/_abap_file_information";
import {ABAPFile} from "../abap/abap_file";
 
export class Symbols {
  private readonly reg: IRegistry;
 
  public constructor(reg: IRegistry) {
    this.reg = reg;
  }
 
  public find(uri: string): LServer.DocumentSymbol[] {
    const file = LSPUtils.getABAPFile(this.reg, uri);
    if (file === undefined) {
      return [];
    }
 
    const ret: LServer.DocumentSymbol[] = [];
    ret.push(...this.outputClasses(file));
    ret.push(...this.outputForms(file));
    return ret;
  }
 
  private selectionRange(identifier: Identifier): LServer.Range {
    const pos = identifier.getStart();
    const str = identifier.getName();
    return LServer.Range.create(pos.getRow() - 1, pos.getCol() - 1, pos.getRow() - 1, pos.getCol() - 1 + str.length);
  }
 
  private range(identifer: Identifier): LServer.Range {
    const start = identifer.getStart();
    const end = identifer.getEnd();
    return LServer.Range.create(start.getRow() - 1, start.getCol() - 1, end.getRow() - 1, end.getCol() - 1);
  }
 
  private newSymbol(identifier: Identifier, kind: LServer.SymbolKind, children: LServer.DocumentSymbol[]): LServer.DocumentSymbol {
    const symbol: LServer.DocumentSymbol = {
      name: identifier.getName(),
      kind: kind,
      range: this.range(identifier),
      selectionRange: this.selectionRange(identifier),
      children,
    };
 
    return symbol;
  }
 
  private outputForms(file: ABAPFile): LServer.DocumentSymbol[] {
    const ret: LServer.DocumentSymbol[] = [];
    for (const form of file.getInfo().listFormDefinitions()) {
      const symbol = this.newSymbol(form.identifier, LServer.SymbolKind.Function, []);
      ret.push(symbol);
    }
    return ret;
  }
 
  private outputClasses(file: ABAPFile): LServer.DocumentSymbol[] {
    const ret: LServer.DocumentSymbol[] = [];
 
    for (const cla of file.getInfo().listClassDefinitions()) {
      const children: LServer.DocumentSymbol[] = [];
      children.push(...this.outputClassAttributes(cla.attributes));
      children.push(...this.outputMethodDefinitions(cla.methods));
      const symbol = this.newSymbol(cla.identifier, LServer.SymbolKind.Class, children);
      ret.push(symbol);
    }
 
    for (const cla of file.getInfo().listClassImplementations()) {
      const children: LServer.DocumentSymbol[] = [];
      children.push(...this.outputMethodImplementations(cla.methods));
      const symbol = this.newSymbol(cla.identifier, LServer.SymbolKind.Class, children);
      ret.push(symbol);
    }
 
    return ret;
  }
 
  private outputMethodImplementations(methods: readonly Identifier[]): LServer.DocumentSymbol[] {
    const ret: LServer.DocumentSymbol[] = [];
    for (const method of methods) {
      const symbol = this.newSymbol(method, LServer.SymbolKind.Method, []);
      ret.push(symbol);
    }
    return ret;
  }
 
  private outputClassAttributes(attr: readonly InfoAttribute[]): LServer.DocumentSymbol[] {
    if (attr === undefined) {
      return [];
    }
    const ret: LServer.DocumentSymbol[] = [];
 
    for (const id of attr) {
      ret.push(this.newSymbol(id.identifier, LServer.SymbolKind.Property, []));
    }
    // todo, also add constants
 
    return ret;
  }
 
  private outputMethodDefinitions(methods: readonly InfoMethodDefinition[]): LServer.DocumentSymbol[] {
    if (methods === undefined) {
      return [];
    }
// todo
    return [];
  }
 
}