All files / src/lsp symbols.ts

93.71% Statements 149/159
77.77% Branches 21/27
100% Functions 11/11
93.71% Lines 149/159

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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 1591x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 1x 1x 7x 7x     7x 7x 7x 7x 7x 7x 7x 1x 1x 9x 9x 9x 9x 1x 1x 7x 7x 7x 7x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 7x 7x 1x 1x 1x 7x 7x 1x 1x 7x 7x 7x 24x 1x 1x     1x 1x 1x 1x 24x 7x 7x 7x 1x 1x 7x 7x 7x 1x 1x 1x 1x 1x 7x 7x 4x 4x 4x 4x 4x 7x 7x 7x 1x 1x 4x 4x 4x 2x 2x 2x 7x 4x 4x 7x 2x 2x 2x 7x 2x 2x     2x 2x 2x 2x 2x 2x 4x 4x 4x 1x 1x 1x     1x 1x 1x     1x 1x 1x 1x 1x 1x
/* eslint-disable max-len */
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} from "../abap/4_file_information/_abap_file_information";
import {ABAPFile} from "../abap/abap_file";
import {EndMethod} from "../abap/2_statements/statements";
import {Position} from "../position";
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
 
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));
    ret.push(...this.outputModules(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 newSymbolRanged(identifier: Identifier, kind: LServer.SymbolKind, children: LServer.DocumentSymbol[], range: LServer.Range): LServer.DocumentSymbol {
    const symbol: LServer.DocumentSymbol = {
      name: identifier.getName(),
      kind: kind,
      range: range,
      selectionRange: this.selectionRange(identifier),
      children,
    };
 
    return symbol;
  }
 
  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 outputModules(file: ABAPFile): LServer.DocumentSymbol[] {
    const ret: LServer.DocumentSymbol[] = [];
 
    for (const statement of file.getStatements()) {
      if (statement.get() instanceof Statements.Module) {
        const nameToken = statement.findFirstExpression(Expressions.FormName)?.getFirstToken();
        if (nameToken === undefined) {
          continue;
        }
        const identifier = new Identifier(nameToken, file.getFilename());
        const symbol = this.newSymbol(identifier, LServer.SymbolKind.Module, []);
        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));
      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, file));
      const symbol = this.newSymbol(cla.identifier, LServer.SymbolKind.Class, children);
      ret.push(symbol);
    }
 
    return ret;
  }
 
  private outputMethodImplementations(methods: readonly Identifier[], file: ABAPFile): LServer.DocumentSymbol[] {
    const ret: LServer.DocumentSymbol[] = [];
 
    for (const method of methods) {
      const start = method.getStart();
      let end: Position | undefined = undefined;
      for (const s of file.getStatements()) {
        if (s.getFirstToken().getStart().isBefore(start)) {
          continue;
        }
        if (s.get() instanceof EndMethod) {
          end = s.getLastToken().getEnd();
          break;
        }
      }
 
      if (end === undefined) {
        continue;
      }
 
      const range = LServer.Range.create(start.getRow() - 1, start.getCol() - 1, end.getRow() - 1, end.getCol() - 1);
 
      const symbol = this.newSymbolRanged(method, LServer.SymbolKind.Method, [], range);
      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;
  }
 
}