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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | 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 1x 1x 1x 1x 1x 155x 155x 2x 2x 2x 153x 153x 155x 1x 1x 152x 152x 155x 1x 1x 1x 1x 1x 1x 1x 1x 152x 152x 152x 152x 155x 1x 1x 151x 151x 155x 6x 6x 6x 6x 6x 6x 6x 6x 6x 145x 145x 155x 2x 2x 2x 2x 2x 2x 2x 2x 2x 143x 143x 155x 8x 8x 8x 8x 135x 135x 155x 8x 8x 8x 8x 8x 8x 8x 8x 127x 127x 127x 155x 13x 3x 3x 13x 127x 127x 155x 32x 32x 1x 32x 31x 31x 32x 32x 32x 32x 32x 32x 32x 95x 95x 155x 73x 36x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 35x 72x 94x 94x 155x 84x 98x 15x 15x 98x 98x 84x 84x 84x 84x 84x 69x 69x 4x 4x 69x 2x 2x 69x 84x 84x 84x 84x 84x 84x 84x 84x 84x 10x 155x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 155x 1x 1x 8x 8x 1x 1x 1x 1x 72x 72x 6x 6x 72x 22x 22x 72x 2x 2x 72x 52x 52x 72x 2x 2x 72x 72x 3x 3x 72x 6x 6x 72x 4x 4x 72x 72x 72x 1x 1x 98x 98x 82x 82x 98x 98x 98x 11x 11x 8x 8x 11x 2x 2x 11x 2x 2x 11x 11x 98x 32x 87x 2x 2x 2x 2x 98x 98x 82x 82x 98x 98x 43x 43x 98x 98x 98x 1x 1x 11x 2x 2x 9x 9x 11x 4x 4x 5x 5x 5x 1x 1x 15x 15x 13x 15x 1x 15x 1x 15x 15x 15x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 8x 8x 5x 7x 7x 7x 7x 7x 7x 7x 7x 7x 4x 4x 7x 7x 7x 7x 7x 1x 1x 12x 12x 5x 5x 12x 12x 1x 1x 352x 352x 352x 266x 98x 98x 266x 352x 352x 352x 258x 258x 352x 352x 352x 1x 1x 2x 2x 2x 2x 2x 1x 1x 135x 135x 135x 135x 135x 118x 118x 17x 135x 135x 17x 135x 1x 1x 16x 16x 16x 135x 8x 8x 8x 135x 7x 7x 135x 1x 1x 1x 135x 1x 1x 153x 152x 152x 1x 1x 153x 1x 1x 1x 1x 153x 1x 1x 1x 1x 1x 155x 153x 153x 2x 2x 155x 2x 2x 2x 2x 155x 2x 2x 2x 2x 2x 1x 1x | /* eslint-disable max-len */
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 {AbstractToken} from "../abap/1_lexer/tokens/abstract_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, Program} from "../objects";
import {IInterfaceDefinition} from "../abap/types/_interface_definition";
import {ABAPFile} from "../abap/abap_file";
import {IMethodDefinition} from "..";
import {FormDefinition} from "../abap/types";
import {MacroCall} from "../abap/2_statements/statements/_statement";
import {IncludeGraph} from "../utils/include_graph";
import {Visibility} from "../abap/4_file_information/visibility";
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};
}
let main = obj;
if (obj instanceof Program && obj.isInclude()) {
// todo: this is slow
const ig = new IncludeGraph(reg);
const mains = ig.listMainForInclude(cursor.identifier.getFilename());
if (mains.length === 1) {
// yea, well, or it has to be a popup
main = reg.findObjectForFile(reg.getFileByName(mains[0])!) as ABAPObject || obj;
}
}
const bottomScope = new SyntaxLogic(reg, main).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.getParent());
if (method !== undefined && method.getStart().equals(cursor.token.getStart())) {
const found = LSPUtils.identiferToLocation(method);
let hover = "Method Definition \"" + method.getName() + "\"";
// Method definitions have getVisibility()
const methodDef = method as IMethodDefinition;
const visibilityStr = this.visibilityToString(methodDef.getVisibility());
hover += "\n\n" + visibilityStr;
return {hover, definition: found, definitionId: method, 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 variable = bottomScope.findVariable(cursor.token.getStr());
if (variable !== undefined && variable.getStart().equals(cursor.token.getStart())) {
const hover = "Variable Definition\n\n" + this.dumpType(variable);
if (hoverValue !== "") {
hoverValue = hover + "\n_________________\n" + hoverValue;
} else {
hoverValue = hover;
}
let location: LServer.Location | undefined = undefined;
if (variable.getMeta().includes(IdentifierMeta.BuiltIn) === false) {
location = LSPUtils.identiferToLocation(variable);
}
return {hover: hoverValue, definition: location, implementation: location, definitionId: variable, scope: bottomScope};
}
// TODO: this can be optimized, no need to loop through all the defintions, the scope knows the name of the object?
for (const c of [...bottomScope.listClassDefinitions(), ...bottomScope.listInterfaceDefinitions()]) {
for (const m of c.getMethodDefinitions()?.getAll() || []) {
for (const p of m.getParameters()?.getAll() || []) {
if (p.getStart().equals(cursor.token.getStart())) {
const found = LSPUtils.identiferToLocation(p);
return {
hover: "Method Parameter: " + cursor.token.getStr().replace("!", ""),
definition: found,
definitionId: p,
implementation: undefined,
scope: bottomScope,
};
}
}
}
}
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 (cursor.snode.get() instanceof MacroCall) {
const macroDefinition = reg.getMacroReferences().findDefinitionByUsage(cursor.identifier.getFilename(), cursor.snode.getFirstToken());
if (macroDefinition) {
return {
hover: "Macro Call",
definition: {
uri: macroDefinition?.filename,
range: LSPUtils.tokenToRange(macroDefinition.token),
},
};
}
}
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().getRTTIName()) {
value += "\n\nRTTI Name: ```" + variable.getType().getRTTIName() + "```";
}
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() + "```";
}
if (variable.getType().getDescription() !== undefined) {
value += "\n\nDescription: " + variable.getType().getDescription();
}
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 = 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 visibilityToString(visibility: Visibility): string {
switch (visibility) {
case Visibility.Public:
return "PUBLIC";
case Visibility.Protected:
return "PROTECTED";
case Visibility.Private:
return "PRIVATE";
default:
return "";
}
}
private static methodParameters(methodDef: IMethodDefinition) {
let ret = "";
// Add visibility information
const visibilityStr = this.visibilityToString(methodDef.getVisibility());
if (visibilityStr) {
ret += visibilityStr + "\n\n";
}
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: AbstractToken): 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 | undefined): Identifier | undefined {
if (scope === undefined) {
return undefined;
}
if ((scope.getIdentifier().stype !== ScopeType.ClassDefinition
&& scope.getIdentifier().stype !== ScopeType.Interface)
|| !(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;
}
if (scope.getIdentifier().stype === ScopeType.ClassDefinition) {
const def = scope.getParent()?.findClassDefinition(scope.getIdentifier().sname)?.getMethodDefinitions()?.getByName(nameToken.getStr());
return def;
} else {
const def = scope.getParent()?.findInterfaceDefinition(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;
}
} |