Created
October 17, 2023 16:09
-
-
Save legendSabbir/eba2e344e21c6cbe39223a277250c557 to your computer and use it in GitHub Desktop.
Path Autocomplete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🤟