All files / src/lsp _lookup.ts

87.5% Statements 140/160
79.58% Branches 113/142
100% Functions 10/10
87.5% Lines 140/160

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 3231x 1x 1x     1x   1x 1x     1x   1x 1x                               1x     85x 85x 2x 2x     83x 83x 1x     82x     82x 1x     81x 81x 6x 6x             75x 75x 2x 2x             73x 73x 4x 4x 4x     69x 69x 2x 2x 2x     67x 67x 10x   10x 10x 10x   10x     57x 57x 52x 52x 60x 8x   60x     52x 52x 44x 44x 4x     52x     5x           29x 29x 3x   29x 7x   29x     29x 3x   29x       29x       60x 60x 52x   60x   60x 6x 6x 4x   6x     6x       6x 54x 15x     60x 9x     60x       6x       6x 6x 2x     4x 4x 2x 2x 2x       4x             4x             4x 4x 2x     4x             4x       4x 4x 4x   4x       201x   201x 121x 60x       201x 201x 144x     201x       2x             69x   64x     5x 5x       5x 1x       4x   2x     2x 2x       83x 82x     1x 1x         1x 1x         1x       85x 83x     2x 2x         2x 2x         2x 2x 2x            
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";
 
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;
      Eif (variable.getMeta().includes(IdentifierMeta.BuiltIn) === false) {
        location = LSPUtils.identiferToLocation(variable);
      }
      return {hover, definition: location, implementation: location, definitionId: variable, scope: bottomScope};
    }
 
    const refs = this.searchReferences(bottomScope, cursor.token);
    if (refs.length > 0) {
      let value = "";
      for (const ref of refs) {
        if (value !== "") {
          value += "\n_________________\n";
        }
        value += this.referenceHover(ref, bottomScope, reg);
      }
 
      let definition: LServer.Location | undefined = undefined;
      if (refs[0].resolved) {
        definition = LSPUtils.identiferToLocation(refs[0].resolved);
        if (definition.uri === BuiltIn.filename) {
          definition = undefined;
        }
      }
      return {hover: value, definition, definitionId: refs[0].resolved, scope: bottomScope};
    }
 
    return undefined;
  }
 
////////////////////////////////////////////
 
  private static dumpType(variable: TypedIdentifier): string {
    let value = variable.toText() + "\n\nType: " + variable.getType().toText(0);
    if (variable.getValue()) {
      value = value + "\n\nValue: ```" + variable.getValue() + "```";
    }
    if (variable.getMeta().length > 0) {
      value = value + "\n\nMeta: " + variable.getMeta().join(", ");
    }
    Iif (variable.getType().containsVoid() === true) {
      value = value + "\n\nContains void types";
    }
    if (variable.getType().getQualifiedName()) {
      value = value + "\n\nQualified type name: ```" + variable.getType().getQualifiedName() + "```";
    }
    Iif (variable.getType().isGeneric() === true) {
      value = value + "\n\nIs generic type";
    }
 
    return value;
  }
 
  private static referenceHover(ref: IReference, scope: ISpaghettiScopeNode, reg: IRegistry): string {
    let name = "";
    if (ref.resolved) {
      name = "```" + ref.resolved.getName() + "```";
    }
    let ret = "Resolved Reference: " + 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);
      }
      Iif (cdef === undefined) {
        cdef = (reg.getObject("CLAS", ref.extra.ooName) as Class | undefined)?.getDefinition();
      }
      Iif (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);
    }
 
    if (ref.extra !== undefined && Object.keys(ref.extra).length > 0) {
      ret += "\n\nExtra: " + JSON.stringify(ref.extra);
    }
 
    return ret;
  }
 
  private static hoverMethod(method: string, def: IClassDefinition | IInterfaceDefinition | undefined): string {
    Iif (def === undefined) {
      return "class not found";
    }
 
    const mdef = def.getMethodDefinitions().getByName(method);
    if (mdef === undefined) {
      return "method not found in definition";
    }
 
    let ret = "";
    if (mdef.getParameters().getImporting().length > 0) {
      ret += "IMPORTING\n";
      for (const p of mdef.getParameters().getImporting()) {
        ret += this.singleParameter(p);
      }
    }
 
    Iif (mdef.getParameters().getExporting().length > 0) {
      ret += "EXPORTING\n";
      for (const p of mdef.getParameters().getExporting()) {
        ret += this.singleParameter(p);
      }
    }
 
    Iif (mdef.getParameters().getChanging().length > 0) {
      ret += "CHANGING\n";
      for (const p of mdef.getParameters().getChanging()) {
        ret += this.singleParameter(p);
      }
    }
 
    const r = mdef.getParameters().getReturning();
    if (r) {
      ret += "RETURNING\n" + this.singleParameter(r);
    }
 
    Iif (mdef.getRaising().length > 0) {
      ret += "RAISING\n";
      for (const p of mdef.getRaising()) {
        ret += "* " + p + "\n";
      }
    }
 
    return ret;
  }
 
  private static singleParameter(p: TypedIdentifier): string {
    let extra = p.getMeta().join(", ");
    Eif (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();
    Iif (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);
    Iif (name === undefined) {
      return undefined;
    }
 
    // check the cursor is at the right token
    const token = name.getFirstToken();
    Iif (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);
    Iif (name === undefined) {
      return undefined;
    }
 
// check the cursor is at the right token
    const token = name.getFirstToken();
    Iif (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;
    Eif (obj) {
      return obj.getABAPFiles()[0];
    }
 
    return undefined;
  }
 
}