All files / src/lsp _lookup.ts

89.36% Statements 336/376
80.86% Branches 93/115
100% Functions 11/11
89.36% Lines 336/376

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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 3761x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 115x 115x 2x 2x 2x 113x 113x 115x 1x 1x 112x 112x 112x 112x 115x 1x 1x 111x 111x 115x 6x 6x 6x 6x 6x 6x 6x 6x 6x 105x 105x 115x 2x 2x 2x 2x 2x 2x 2x 2x 2x 103x 103x 115x 7x 7x 7x 7x 96x 96x 115x 2x 2x 2x 2x 94x 94x 115x 20x 20x 20x 20x 20x 20x 20x 20x 74x 74x 74x 115x 7x 2x 2x 7x 74x 74x 115x 66x 77x 12x 12x 77x 77x 66x 66x 66x 66x 66x 53x 53x 5x 5x 53x 2x 2x 53x 66x 66x 66x 66x 66x 66x 66x 66x 66x 8x 115x 1x 1x 7x 7x 1x 1x 1x 1x 49x 49x 3x 3x 49x 12x 12x 49x     49x 37x 37x 49x     49x 3x 3x 49x 4x 4x 49x 49x 49x 1x 1x 77x 77x 64x 64x 77x 77x 77x 8x 8x 6x 6x 8x     8x     8x 8x 77x 22x 69x 2x 2x     2x 2x 77x 77x 64x 64x 77x 77x 23x 23x 77x 77x 77x 1x 1x 8x     8x 8x 8x 4x 4x 4x 4x 4x 1x 1x 6x 6x 6x 6x 6x 4x 4x 7x 7x 4x 6x 6x 6x           6x 6x 6x           6x 6x 6x 4x 4x 6x 6x           6x 6x 6x 1x 1x 11x 11x 4x 4x 11x 11x 1x 1x 267x 267x 267x 157x 77x 77x 157x 267x 267x 267x 193x 193x 267x 267x 267x 1x 1x 2x 2x 2x 2x 2x 1x 1x 96x 96x 89x 89x 7x 96x 96x     7x 96x 1x 1x 6x 6x 6x 96x 4x 4x 2x 96x 96x 96x 1x 1x 113x 112x 112x 1x 1x 113x     1x 1x 1x 1x 113x     1x 1x 1x 1x 1x 115x 113x 113x 2x 2x 115x     2x 2x 2x 2x 115x     2x 2x 2x 2x 2x       1x 1x
import * as LServer from "vscode-languageserver-types";
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {IRegistry} from "../_iregistry";
import {ABAPObject} from "../objects/_abap_object";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ISpaghettiScopeNode} from "../abap/5_syntax/_spaghetti_scope";
import {ICursorData, LSPUtils} from "./_lsp_utils";
import {TypedIdentifier, IdentifierMeta} from "../abap/types/_typed_identifier";
import {Identifier} from "../abap/4_file_information/_identifier";
import {Token} from "../abap/1_lexer/tokens/_token";
import {IReference, ReferenceType} from "../abap/5_syntax/_reference";
import {IClassDefinition} from "../abap/types/_class_definition";
import {BuiltIn} from "../abap/5_syntax/_builtin";
import {ScopeType} from "../abap/5_syntax/_scope_type";
import {Class, Interface} from "../objects";
import {IInterfaceDefinition} from "../abap/types/_interface_definition";
import {ABAPFile} from "../abap/abap_file";
import {IMethodDefinition} from "..";
import {FormDefinition} from "../abap/types";
 
export interface LSPLookupResult {
  /** in markdown */
  hover: string | undefined;
  /** used for go to definition */
  definition?: LServer.Location | undefined;
  /** used for go to implementation */
  implementation?: LServer.Location | undefined;
  definitionId?: Identifier;
  scope?: ISpaghettiScopeNode;
}
 
export class LSPLookup {
 
  public static lookup(cursor: ICursorData, reg: IRegistry, obj: ABAPObject): LSPLookupResult | undefined {
    const inc = this.findInclude(cursor, reg);
    if (inc) {
      const found = this.ABAPFileResult(inc);
      return {hover: "Include", definition: found, implementation: found};
    }
 
    const fm = this.findFunctionModule(cursor);
    if (fm) {
      return {hover: "Function Module " + fm};
    }
 
    const bottomScope = new SyntaxLogic(reg, obj).run().spaghetti.lookupPosition(
      cursor.identifier.getStart(),
      cursor.identifier.getFilename());
    if (bottomScope === undefined) {
      return undefined;
    }
 
    const clas = bottomScope.findClassDefinition(cursor.token.getStr());
    if (clas && clas.getStart().equals(cursor.token.getStart())) {
      const found = LSPUtils.identiferToLocation(clas);
      return {
        hover: "Class Definition, " + cursor.token.getStr(),
        definition: found,
        definitionId: clas,
        implementation: undefined,
        scope: bottomScope,
      };
    }
 
    const intf = bottomScope.findInterfaceDefinition(cursor.token.getStr());
    if (intf && intf.getStart().equals(cursor.token.getStart())) {
      const found = LSPUtils.identiferToLocation(intf);
      return {
        hover: "Interface Definition, " + cursor.token.getStr(),
        definition: found,
        definitionId: intf,
        implementation: undefined,
        scope: bottomScope,
      };
    }
 
    const type = bottomScope.findType(cursor.token.getStr());
    if (type !== undefined && type.getStart().equals(cursor.token.getStart())) {
      const found = LSPUtils.identiferToLocation(type);
      const hover = "Type Definition, " + cursor.token.getStr() + "\n\n" + this.dumpType(type);
      return {hover, definition: found, definitionId: type, scope: bottomScope};
    }
 
    const method = this.findMethodDefinition(cursor, bottomScope);
    if (method !== undefined && method.getStart().equals(cursor.token.getStart())) {
      const found = LSPUtils.identiferToLocation(method);
      const hover = "Method Definition \"" + method.getName() + "\"";
      return {hover, definition: found, definitionId: method, scope: bottomScope};
    }
 
    const variable = bottomScope.findVariable(cursor.token.getStr());
    if (variable !== undefined && variable.getStart().equals(cursor.token.getStart())) {
      const hover = "Variable Definition\n\n" + this.dumpType(variable);
 
      let location: LServer.Location | undefined = undefined;
      if (variable.getMeta().includes(IdentifierMeta.BuiltIn) === false) {
        location = LSPUtils.identiferToLocation(variable);
      }
      return {hover, definition: location, implementation: location, definitionId: variable, scope: bottomScope};
    }
 
    let hoverValue = "";
    const ddicRefs = reg.getDDICReferences().listByFilename(cursor.identifier.getFilename(), cursor.identifier.getStart().getRow());
    for (const d of ddicRefs) {
      if (d.object && d.token && d.token.getStart().equals(cursor.identifier.getStart())) {
        hoverValue += `DDIC: ${d.object.getType()} ${d.object.getName()}`;
      }
    }
 
    const refs = this.searchReferences(bottomScope, cursor.token);
    if (refs.length > 0) {
      for (const ref of refs) {
        if (hoverValue !== "") {
          hoverValue += "\n_________________\n";
        }
        hoverValue += this.referenceHover(ref, bottomScope, reg);
      }
 
      let definition: LServer.Location | undefined = undefined;
      let implementation: LServer.Location | undefined = undefined;
 
      if (refs[0].resolved) {
        definition = LSPUtils.identiferToLocation(refs[0].resolved);
        if (definition.uri === BuiltIn.filename) {
          definition = undefined;
        }
        if (refs[0].resolved instanceof FormDefinition) {
          implementation = definition;
        }
      }
 
      return {
        hover: hoverValue,
        definition: definition,
        implementation: implementation,
        definitionId: refs[0].resolved,
        scope: bottomScope,
      };
    }
 
    if (hoverValue !== "") {
      return {hover: hoverValue, scope: bottomScope};
    }
    return undefined;
  }
 
  ////////////////////////////////////////////
 
  private static dumpType(variable: TypedIdentifier): string {
    let value = variable.toText() + "\n\nType: " + variable.getType().toText(0);
    if (variable.getValue()) {
      value += "\n\nValue: ```" + variable.getValue() + "```";
    }
    if (variable.getMeta().length > 0) {
      value += "\n\nMeta: " + variable.getMeta().join(", ");
    }
    if (variable.getType().containsVoid() === true) {
      value += "\n\nContains Void types";
    }
    if (variable.getType().getQualifiedName()) {
      value += "\n\nQualified Type Name: ```" + variable.getType().getQualifiedName() + "```";
    }
    if (variable.getType().isGeneric() === true) {
      value += "\n\nIs Generic Type";
    }
    if (variable.getType().getConversionExit() !== undefined) {
      value += "\n\nConversion Exit: ```" + variable.getType().getConversionExit() + "```";
    }
    if (variable.getType().getDDICName() !== undefined) {
      value += "\n\nDDIC Name: ```" + variable.getType().getDDICName() + "```";
    }
 
    return value;
  }
 
  private static referenceHover(ref: IReference, scope: ISpaghettiScopeNode, reg: IRegistry): string {
    let name = "";
    if (ref.resolved) {
      name = "```" + ref.resolved.getName() + "```";
    }
    let ret = `${ref.referenceType} ${name}`;
 
    if (ref.referenceType === ReferenceType.MethodReference && ref.extra?.ooName) {
      let cdef: IClassDefinition | IInterfaceDefinition | undefined = scope.findClassDefinition(ref.extra.ooName);
      if (cdef === undefined) {
        cdef = scope.findInterfaceDefinition(ref.extra.ooName);
      }
      if (cdef === undefined) {
        cdef = (reg.getObject("CLAS", ref.extra.ooName) as Class | undefined)?.getDefinition();
      }
      if (cdef === undefined) {
        cdef = (reg.getObject("INTF", ref.extra.ooName) as Interface | undefined)?.getDefinition();
      }
 
      ret += "\n\n" + this.hoverMethod(ref.position.getName(), cdef);
    } else if (ref.resolved instanceof TypedIdentifier) {
      ret += "\n\n" + this.dumpType(ref.resolved);
    } else if (ref.referenceType === ReferenceType.BuiltinMethodReference) {
      const builtinDef = new BuiltIn().searchBuiltin(ref.resolved?.getName()?.toUpperCase());
      if (builtinDef === undefined) {
        return "Error: builtin method signature not found";
      }
      ret += "\n\n" + this.methodParameters(builtinDef);
    }
 
    if (ref.resolved) {
      ret += "\n\n(Resolved)";
    }
 
    if (ref.extra !== undefined && Object.keys(ref.extra).length > 0) {
      ret += "\n\nExtra: " + JSON.stringify(ref.extra);
    }
 
    return ret;
  }
 
  private static hoverMethod(method: string, classDef: IClassDefinition | IInterfaceDefinition | undefined): string {
    if (classDef === undefined) {
      return "class not found";
    }
 
    const methodDef = classDef.getMethodDefinitions().getByName(method);
    if (methodDef === undefined) {
      return "method not found in definition";
    }
 
    return this.methodParameters(methodDef);
  }
 
  private static methodParameters(methodDef: IMethodDefinition) {
    let ret = "";
    const parameters = methodDef.getParameters();
 
    const importing = parameters.getImporting();
    if (importing.length > 0) {
      ret += "IMPORTING\n";
      for (const p of importing) {
        ret += this.singleParameter(p);
      }
    }
 
    const exporting = parameters.getExporting();
    if (exporting.length > 0) {
      ret += "EXPORTING\n";
      for (const p of exporting) {
        ret += this.singleParameter(p);
      }
    }
 
    const changing = parameters.getChanging();
    if (changing.length > 0) {
      ret += "CHANGING\n";
      for (const p of changing) {
        ret += this.singleParameter(p);
      }
    }
 
    const r = parameters.getReturning();
    if (r) {
      ret += "RETURNING\n" + this.singleParameter(r);
    }
 
    if (methodDef.getRaising().length > 0) {
      ret += "RAISING\n";
      for (const p of methodDef.getRaising()) {
        ret += "* " + p + "\n";
      }
    }
 
    return ret;
  }
 
  private static singleParameter(p: TypedIdentifier): string {
    let extra = p.getMeta().join(", ");
    if (extra !== "") {
      extra = "(Meta: " + extra + ")";
    }
    return "* " + p.getName() + extra + " TYPE " + p.getType().toText(1) + "\n\n";
  }
 
  private static searchReferences(scope: ISpaghettiScopeNode, token: Token): IReference[] {
    const ret: IReference[] = [];
 
    for (const r of scope.getData().references) {
      if (r.position.getStart().equals(token.getStart())) {
        ret.push(r);
      }
    }
 
    const parent = scope.getParent();
    if (parent) {
      ret.push(...this.searchReferences(parent, token));
    }
 
    return ret;
  }
 
  private static ABAPFileResult(abap: ABAPFile): LServer.Location {
    return {
      uri: abap.getFilename(),
      range: LServer.Range.create(0, 0, 0, 0),
    };
  }
 
  private static findMethodDefinition(found: ICursorData, scope: ISpaghettiScopeNode): Identifier | undefined {
    if (scope.getIdentifier().stype !== ScopeType.ClassDefinition
      || !(found.snode.get() instanceof Statements.MethodDef)) {
      return undefined;
    }
 
    const nameToken = found.snode.findFirstExpression(Expressions.MethodName)?.getFirstToken();
    if (nameToken === undefined) {
      return undefined;
    }
 
    if (found.snode.findFirstExpression(Expressions.Redefinition)) {
      return undefined;
    }
 
    // check the cursor is at the right token
    if (nameToken.getStart().getCol() !== found.token.getStart().getCol()
      || nameToken.getStart().getRow() !== found.token.getStart().getRow()) {
      return undefined;
    }
 
    const def = scope.getParent()?.findClassDefinition(scope.getIdentifier().sname)?.getMethodDefinitions()?.getByName(nameToken.getStr());
    return def;
  }
 
  private static findFunctionModule(found: ICursorData): string | undefined {
    if (!(found.snode.get() instanceof Statements.CallFunction)) {
      return undefined;
    }
 
    const name = found.snode.findFirstExpression(Expressions.FunctionName);
    if (name === undefined) {
      return undefined;
    }
 
    // check the cursor is at the right token
    const token = name.getFirstToken();
    if (token.getStart().getCol() !== found.token.getStart().getCol()
      || token.getStart().getRow() !== found.token.getStart().getRow()) {
      return undefined;
    }
 
    return token.getStr();
  }
 
  private static findInclude(found: ICursorData, reg: IRegistry): ABAPFile | undefined {
    if (!(found.snode.get() instanceof Statements.Include)) {
      return;
    }
 
    const name = found.snode.findFirstExpression(Expressions.IncludeName);
    if (name === undefined) {
      return undefined;
    }
 
    // check the cursor is at the right token
    const token = name.getFirstToken();
    if (token.getStart().getCol() !== found.token.getStart().getCol()
      || token.getStart().getRow() !== found.token.getStart().getRow()) {
      return undefined;
    }
 
    const obj = reg.getObject("PROG", token.getStr()) as ABAPObject | undefined;
    if (obj) {
      return obj.getABAPFiles()[0];
    }

    return undefined;
  }
 
}