export async function findInterface()

in src/utils.ts [232:297]


export async function findInterface(editor: vscode.TextEditor, hasMultiOrigins: boolean, pontManager: PontManager) {
  const pos = editor.selection.start;
  const codeAtLine = editor.document.getText().split("\n")[pos.line];

  if (!codeAtLine) {
    return Promise.reject(new Error(I18N.template(I18N.src.utils.cannotFindApi, { val1: codeAtLine })));
  }

  const words = codeAtLine.split(".");

  if (words.length < 2) {
    return Promise.reject(new Error(I18N.template(I18N.src.utils.cannotFindApi2, { val1: words })));
  }

  let wordIndex = 0;
  let chPos = 0;

  for (let index = 0; index < words.length; ++index) {
    const word = words[index];

    if (chPos + word.length > pos.character) {
      wordIndex = index;

      break;
    }

    chPos += word.length;
    // add . length
    chPos++;
  }

  if (wordIndex === 0) {
    return;
  }

  const wordsWithOrigin = [words[wordIndex - 2], words[wordIndex - 1], words[wordIndex]];
  const localSpecs = pontManager.localPontSpecs;

  let offsetIndex = 1;
  let specIndex = 0;
  if (hasMultiOrigins) {
    offsetIndex = 0;
    specIndex = localSpecs?.findIndex((spec) => spec.name === wordsWithOrigin[offsetIndex]);

    if (specIndex < 0) {
      offsetIndex = 1;
      specIndex = localSpecs?.findIndex((spec) => spec.name === wordsWithOrigin[offsetIndex]);
    }
    if (specIndex < 0) {
      offsetIndex = 1;
      specIndex = 0;
    }
  }

  const apiKey = wordsWithOrigin.slice(offsetIndex + 1).join("/");
  const nameWords = wordsWithOrigin.slice(offsetIndex + 1);
  const api = PontJsonPointer.get(localSpecs, `${specIndex}.apis.[${apiKey}]`);
  if (api) {
    return {
      specName: localSpecs[specIndex]?.name || "",
      apiName: api?.name,
      apiKey,
      modName: nameWords?.length > 1 ? nameWords[0] : "",
    };
  }
}