Skip to content

Instantly share code, notes, and snippets.

@legendSabbir
Created October 17, 2023 16:09
Show Gist options
  • Select an option

  • Save legendSabbir/eba2e344e21c6cbe39223a277250c557 to your computer and use it in GitHub Desktop.

Select an option

Save legendSabbir/eba2e344e21c6cbe39223a277250c557 to your computer and use it in GitHub Desktop.
Path Autocomplete
import plugin from "../plugin.json";
const { editor } = editorManager;
const fsOperation = acode.require("fsOperation");
const helpers = acode.require("helpers");
const TokenIterator = ace.require("ace/token_iterator").TokenIterator;
const cache = Object.create(null);
const pathCompleter = {
id: "path-completion",
getCompletions: async function (editor, session, pos, prefix, callback) {
const iterator = new TokenIterator(session, pos.row, pos.column);
const token = iterator.getCurrentToken();
let url = editorManager.activeFile.location;
if ((token?.type.indexOf("string") === -1) || !url)
return;
if (url.endsWith("/")) {
url = url.substring(0, url.length - 1);
}
let value = token.value.replaceAll(/[`'"]/g, "").trim();
if (value.startsWith("../")) {
while (value.indexOf("../") !== -1) {
url = url.substring(0, url.lastIndexOf("/"))
value = value.replace("../", "")
}
}
if (value.indexOf("/") !== -1) {
url = url + "/" + value.substring(0, value.lastIndexOf("/"))
}
try {
let allFiles = cache[url]
if (!allFiles) {
const dir = await fsOperation(url);
if (!await dir.exists()) return
allFiles = await dir.lsDir();
if (allFiles)
cache[url] = allFiles
else
return
}
callback(null, allFiles.map(file => {
const { isFile, name } = file
const completion = {
caption: name,
meta: isFile ? "File" : "Folder",
value: name,
score: 6000,
}
if (extraSyntaxHighlightsInstalled)
completion.icon = isFile ? helpers.getIconForFile(name) : "icon folder";
return completion
}));
} catch (err) {
console.error(err);
}
}
};
function MAIN() {
editor.completers.push(pathCompleter);
}
function DESTROY() {
editor.completers.splice(editor.completers.indexOf(pathCompleter), 1);
}
if (window.acode) {
acode.setPluginInit(plugin.id, MAIN);
acode.setPluginUnmount(plugin.id, DESTROY);
}
@av1934413
Copy link

👍

@av1934413
Copy link


@ElVanze
Copy link

ElVanze commented Jan 12, 2024

@Rain4unow
Copy link

🤟

@zapping5454
Copy link

Yd

@Cali-eze
Copy link

Cali-eze commented Apr 5, 2025

Sharp 💯

@rahulkumarnukhiya
Copy link

So Good

@rahulkumarnukhiya
Copy link

Good

@Wshao777
Copy link

Wshao777 commented Mar 3, 2026

import plugin from "../plugin.json";

const { editor } = editorManager;
const fsOperation = acode.require("fsOperation");
const helpers = acode.require("helpers");
const TokenIterator = ace.require("ace/token_iterator").TokenIterator;

const cache = Object.create(null);

/**

  • 路徑補全核心
    */
    const pathCompleter = {
    id: "path-completer",

async getCompletions(editor, session, pos, prefix, callback) {
try {
const iterator = new TokenIterator(session, pos.row, pos.column);
const token = iterator.getCurrentToken();

  let url = editorManager.activeFile?.location;
  if (!token || token.type?.indexOf("string") === -1 || !url) return;

  if (url.endsWith("/")) url = url.slice(0, -1);

  let value = token.value.replaceAll(/[`'"]/g, "").trim();

  // 相對路徑解析
  if (value.startsWith("../")) {
    while (value.indexOf("../") !== -1) {
      url = url.substring(0, url.lastIndexOf("/"));
      value = value.replace("../", "");
    }
  }

  // 絕對路徑拼接
  if (value.includes("/")) {
    url = url + "/" + value.substring(0, value.lastIndexOf("/"));
  }

  let allFiles = cache[url];

  if (!allFiles) {
    const dir = await fsOperation(url);
    if (!(await dir.exists())) return;

    allFiles = await dir.lsDir();

    if (allFiles) cache[url] = allFiles;
    else return;
  }

  callback(
    null,
    allFiles.map(file => {
      const { isFile, name } = file;

      return {
        caption: name,
        meta: isFile ? "file" : "folder",
        value: name,
        score: 6000,
        icon:
          window.extraSyntaxHighlights && isFile
            ? helpers.getIconForFile(name)
            : "icon-folder"
      };
    })
  );
} catch (err) {
  console.error("path completer error:", err);
}

}
};

/**

  • 插件初始化
    */
    function MAIN() {
    if (!editor.completers.includes(pathCompleter)) {
    editor.completers.push(pathCompleter);
    }
    }

/**

  • 插件卸載
    */
    function DESTROY() {
    const index = editor.completers.indexOf(pathCompleter);
    if (index !== -1) editor.completers.splice(index, 1);
    }

if (window.acode) {
acode.setPluginInit(plugin.id, MAIN);
acode.setPluginUnmount(plugin.id, DESTROY);
}

@Wshao777
Copy link

Wshao777 commented Mar 3, 2026

613432

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment