All files / src/lsp help.ts

83.08% Statements 226/272
85.29% Branches 29/34
93.75% Functions 15/16
83.08% Lines 226/272

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 2721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x   4x 4x 4x 4x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x     3x 4x 4x 4x 4x 1x 1x 3x 3x 3x 3x 3x                 1x 1x                                                                 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 4x 3x 3x 4x 4x 1x 1x 18x 18x 18x 18x 18x 18x 18x 1x 1x 14x 14x 14x 1x 1x 3x 3x 3x 1x 1x 2x 2x 2x 1x 1x 9x 9x 20x 20x 20x 20x 18x 18x 18x 20x 2x 2x 20x   20x 20x 20x 20x 9x 9x 1x 1x 4x 4x 18x 18x 18x 18x 18x 18x 4x 4x 4x 1x 1x 4x 4x 4x 7x 7x 7x 7x 7x 7x 7x 4x 4x 4x 1x 1x 4x 4x 7x 3x 7x 4x 4x 7x 4x 4x 1x 1x 4x 4x 4x     4x 4x 4x 4x 4x 4x 4x 4x 1x 1x
import * as LServer from "vscode-languageserver-types";
import {IRegistry} from "../_iregistry";
import {INode} from "../abap/nodes/_inode";
import {StructureNode, StatementNode, TokenNodeRegex, ExpressionNode, TokenNode} from "../abap/nodes";
import {AbstractToken} from "../abap/1_lexer/tokens/abstract_token";
import {LSPUtils} from "./_lsp_utils";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ABAPObject} from "../objects/_abap_object";
import {DumpScope} from "./dump_scope";
import {ABAPFile} from "../abap/abap_file";
 
export class Help {
  public static find(reg: IRegistry, textDocument: LServer.TextDocumentIdentifier, position: LServer.Position): string {
 
    const file = LSPUtils.getABAPFile(reg, textDocument.uri);
    if (file === undefined) {
      return "file not found";
    } else {
      return this.dumpABAP(file, reg, textDocument, position);
    }
  }
 
/////////////////////////////////////////////////
 
  private static dumpABAP(file: ABAPFile, reg: IRegistry, textDocument: LServer.TextDocumentIdentifier,
                          position: LServer.Position): string {
 
    let content = "";
 
    content = `
    <a href="#_tokens" rel="no-refresh">Tokens</a> |
    <a href="#_statements" rel="no-refresh">Statements</a> |
    <a href="#_structure" rel="no-refresh">Structure</a> |
    <a href="#_files" rel="no-refresh">Files</a> |
    <a href="#_info" rel="no-refresh">Info Dump</a>
    <hr>
    ` +
      "<tt>" + textDocument.uri + " (" +
      (position.line + 1) + ", " +
      (position.character + 1) + ")</tt>";
 
    content = content + "<hr>";
    content = content + this.cursorInformation(reg, textDocument, position, file);
    content = content + this.fileInformation(file);
    content = content + "<hr>";
    content = content + this.dumpFiles(reg);
    content = content + "<hr>";
    content = content + this.dumpInfo(file);
 
    return content;
  }
 
  private static dumpInfo(file: ABAPFile): string {
    const info = file.getInfo();
 
    const dump = {
      classDefinitions: info.listClassDefinitions(),
      classImplementations: info.listClassImplementations(),
      interfaceDefinitions: info.listInterfaceDefinitions(),
      forms: info.listFormDefinitions(),
    };
 
    const text = JSON.stringify(dump, null, 2);
 
    return `<h3 id="_info">Info Dump</h3><pre>` + text + "</pre>";
  }
 
  private static cursorInformation(reg: IRegistry,
                                   textDocument: LServer.TextDocumentIdentifier,
                                   position: LServer.Position,
                                   file: ABAPFile): string {
    let ret = "";
    const found = LSPUtils.findCursor(reg, {textDocument, position});
 
    if (found !== undefined) {
      ret = "Statement: " + this.linkToStatement(found.snode.get()) + "<br>\n" +
        "Token: " + found.token.constructor.name + "<br>\n" +
        this.fullPath(file, found.token).value;
    } else {
      ret = "No token found at cursor position";
    }
 
    const obj = reg.getObject(file.getObjectType(), file.getObjectName());
    if (obj instanceof ABAPObject) {
      const spaghetti = new SyntaxLogic(reg, obj).run().spaghetti;
      ret = ret + DumpScope.dump(spaghetti);
 
      if (found !== undefined) {
        ret = ret + "<hr>Spaghetti Scope by Cursor Position:<br><br>\n";
        const lookup = spaghetti.lookupPosition(found.token.getStart(), textDocument.uri);
        if (lookup) {
          const identifier = lookup.getIdentifier();
          ret = ret + "<u>" + identifier.stype + ", <tt>" + identifier.sname + "</tt>, " + identifier.filename;
          ret = ret + ", (" + identifier.start.getRow() + ", " + identifier.start.getCol() + ")</u><br>";
        } else {
          ret = ret + "Not found";
        }
      }
    }
 
    return ret;
  }
 
  private static fullPath(file: ABAPFile, token: AbstractToken): {value: string, keyword: boolean} {
    const structure = file.getStructure();
 
    if (structure === undefined) {
      return {value: "", keyword: false};
    }

    const found = this.traverse(structure, "", token);
    if (found === undefined) {
      return {value: "", keyword: false};
    }

    return {value: "\n\n" + found.value, keyword: found.keyword};
  }
 
  private static traverse(node: INode, parents: string, search: AbstractToken): {value: string, keyword: boolean} | undefined {
    let local = parents;
    if (local !== "") {
      local = local + " -> ";
    }
    if (node instanceof StructureNode) {
      local = local + "Structure: " + this.linkToStructure(node.get());
    } else if (node instanceof StatementNode) {
      local = local + "Statement: " + this.linkToStatement(node.get());
    } else if (node instanceof ExpressionNode) {
      local = local + "Expression: " + this.linkToExpression(node.get());
    } else if (node instanceof TokenNode) {
      local = local + "Token: " + node.get().constructor.name;
      const token = node.get();
      if (token.getStr() === search.getStr()
          && token.getCol() === search.getCol()
          && token.getRow() === search.getRow()) {
        const keyword = !(node instanceof TokenNodeRegex);
        return {value: local, keyword};
      }
    } else {
      throw new Error("hover, traverse, unexpected node type");
    }

    for (const child of node.getChildren()) {
      const ret = this.traverse(child, local, search);
      if (ret) {
        return ret;
      }
    }

    return undefined;
  }
 
  private static fileInformation(file: ABAPFile): string {
    let content = "";
 
    content = content + `<hr><h3 id="_tokens">Tokens</h3>\n`;
    content = content + this.tokens(file);
    content = content + `<hr><h3 id="_statements">Statements</h3>\n`;
    content = content + this.buildStatements(file);
    content = content + `<hr><h3 id="_structure">Structure</h3>\n`;
 
    const structure = file.getStructure();
    if (structure !== undefined) {
      content = content + this.buildStructure([structure]);
    } else {
      content = content + "structure undefined";
    }
    return content;
  }
 
  private static escape(str: string) {
    str = str.replace(/&/g, "&amp;");
    str = str.replace(/>/g, "&gt;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/"/g, "&quot;");
    str = str.replace(/'/g, "&#039;");
    return str;
  }
 
  private static linkToStatement(statement: any) {
    return `<a href="https://syntax.abaplint.org/#/statement/${
      statement.constructor.name}" target="_blank">${statement.constructor.name}</a>\n`;
  }
 
  private static linkToStructure(structure: any) {
    return `<a href="https://syntax.abaplint.org/#/structure/${
      structure.constructor.name}" target="_blank">${structure.constructor.name}</a>\n`;
  }
 
  private static linkToExpression(expression: any) {
    return `<a href="https://syntax.abaplint.org/#/expression/${
      expression.constructor.name}" target="_blank">${expression.constructor.name}</a>\n`;
  }
 
  private static outputNodes(nodes: readonly INode[]) {
    let ret = "<ul>";
    for (const node of nodes) {
      let extra = "";
      switch (node.constructor.name) {
        case "TokenNode":
        case "TokenNodeRegex":
          // @ts-ignore
          extra = node.get().constructor.name + ", \"" + node.get().getStr() + "\"";
          break;
        case "ExpressionNode":
          extra = this.linkToExpression(node.get()) + this.outputNodes(node.getChildren());
          break;
        default:
          break;
      }
 
      ret = ret + "<li>" + node.constructor.name + ", " + extra + "</li>";
    }
    return ret + "</ul>";
  }
 
  private static tokens(file: ABAPFile) {
    let inner = "<table><tr><td><b>String</b></td><td><b>Type</b></td><td><b>Row</b></td><td><b>Column</b></td></tr>";
    for (const token of file.getTokens()) {
      inner = inner + "<tr><td><tt>" +
        this.escape(token.getStr()) + "</tt></td><td>" +
        token.constructor.name + "</td><td align=\"right\">" +
        token.getRow() + "</td><td align=\"right\">" +
        token.getCol() + "</td></tr>";
    }
    inner = inner + "</table>";
    return inner;
  }
 
  private static buildStatements(file: ABAPFile) {
    let output = "";
 
    for (const statement of file.getStatements()) {
      const row = statement.getStart().getRow();
  // getting the class name only works if uglify does not mangle names
      output = output +
        row + ": " +
        this.linkToStatement(statement.get()) +
        "</div></b>\n" + this.outputNodes(statement.getChildren());
    }
 
    return output;
  }
 
  private static buildStructure(nodes: readonly INode[]) {
    let output = "<ul>";
    for (const node of nodes) {
      if (node instanceof StructureNode) {
        output = output + "<li>" + this.linkToStructure(node.get()) + ", Structure " + this.buildStructure(node.getChildren()) + "</li>";
      } else if (node instanceof StatementNode) {
        output = output + "<li>" + this.linkToStatement(node.get()) + ", Statement</li>";
      }
    }
    return output + "</ul>";
  }
 
  private static dumpFiles(reg: IRegistry) {
    let output = `<h3 id="_files">Files</h3><table>\n`;
    for (const o of reg.getObjects()) {
      if (reg.isDependency(o) === true) {
        continue;
      }
      output = output + "<tr><td valign=\"top\">" + o.getType() + " " + o.getName() + "</td><td>";
      for (const f of o.getFiles()) {
        output = output + f.getFilename() + "<br>";
      }
      output = output + "</td></tr>\n";
    }
    return output + "</table>\n";
  }
 
}