-
-
Notifications
You must be signed in to change notification settings - Fork 33
Revert "Revert "Support import_map.json[c] in denops plugin directory""
#450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| import type { Denops, Entrypoint } from "jsr:@denops/core@^7.0.0"; | ||
| import { | ||
| type ImportMap, | ||
| ImportMapImporter, | ||
| loadImportMap, | ||
| } from "jsr:@lambdalisue/import-map-importer@^0.3.1"; | ||
| import { toFileUrl } from "jsr:@std/path@^1.0.2/to-file-url"; | ||
| import { fromFileUrl } from "jsr:@std/path@^1.0.2/from-file-url"; | ||
| import { join } from "jsr:@std/path@^1.0.2/join"; | ||
| import { dirname } from "jsr:@std/path@^1.0.2/dirname"; | ||
|
|
||
| type PluginModule = { | ||
| main: Entrypoint; | ||
| }; | ||
|
|
||
| export class Plugin { | ||
| #denops: Denops; | ||
| #loadedWaiter: Promise<void>; | ||
| #unloadedWaiter?: Promise<void>; | ||
| #disposable: AsyncDisposable = voidAsyncDisposable; | ||
|
|
||
| readonly name: string; | ||
| readonly script: string; | ||
|
|
||
| constructor(denops: Denops, name: string, script: string) { | ||
| this.#denops = denops; | ||
| this.name = name; | ||
| this.script = resolveScriptUrl(script); | ||
| this.#loadedWaiter = this.#load(); | ||
| } | ||
|
|
||
| waitLoaded(): Promise<void> { | ||
| return this.#loadedWaiter; | ||
| } | ||
|
|
||
| async #load(): Promise<void> { | ||
| await emit(this.#denops, `DenopsSystemPluginPre:${this.name}`); | ||
| try { | ||
| const mod: PluginModule = await importPlugin(this.script); | ||
| this.#disposable = await mod.main(this.#denops) ?? voidAsyncDisposable; | ||
| } catch (e) { | ||
| // Show a warning message when Deno module cache issue is detected | ||
| // https://github.com/vim-denops/denops.vim/issues/358 | ||
| if (isDenoCacheIssueError(e)) { | ||
| console.warn("*".repeat(80)); | ||
| console.warn(`Deno module cache issue is detected.`); | ||
| console.warn( | ||
| `Execute 'call denops#cache#update(#{reload: v:true})' and restart Vim/Neovim.`, | ||
| ); | ||
| console.warn( | ||
| `See https://github.com/vim-denops/denops.vim/issues/358 for more detail.`, | ||
| ); | ||
| console.warn("*".repeat(80)); | ||
| } | ||
| console.error(`Failed to load plugin '${this.name}': ${e}`); | ||
| await emit(this.#denops, `DenopsSystemPluginFail:${this.name}`); | ||
| throw e; | ||
| } | ||
| await emit(this.#denops, `DenopsSystemPluginPost:${this.name}`); | ||
| } | ||
|
|
||
| unload(): Promise<void> { | ||
| if (!this.#unloadedWaiter) { | ||
| this.#unloadedWaiter = this.#unload(); | ||
| } | ||
| return this.#unloadedWaiter; | ||
| } | ||
|
|
||
| async #unload(): Promise<void> { | ||
| try { | ||
| // Wait for the load to complete to make the events atomically. | ||
| await this.#loadedWaiter; | ||
| } catch { | ||
| // Load failed, do nothing | ||
| return; | ||
| } | ||
| const disposable = this.#disposable; | ||
| this.#disposable = voidAsyncDisposable; | ||
| await emit(this.#denops, `DenopsSystemPluginUnloadPre:${this.name}`); | ||
| try { | ||
| await disposable[Symbol.asyncDispose](); | ||
| } catch (e) { | ||
| console.error(`Failed to unload plugin '${this.name}': ${e}`); | ||
| await emit(this.#denops, `DenopsSystemPluginUnloadFail:${this.name}`); | ||
| return; | ||
| } | ||
| await emit(this.#denops, `DenopsSystemPluginUnloadPost:${this.name}`); | ||
| } | ||
|
|
||
| async call(fn: string, ...args: unknown[]): Promise<unknown> { | ||
| try { | ||
| return await this.#denops.dispatcher[fn](...args); | ||
| } catch (err) { | ||
| const errMsg = err instanceof Error | ||
| ? err.stack ?? err.message // Prefer 'stack' if available | ||
| : String(err); | ||
| throw new Error( | ||
| `Failed to call '${fn}' API in '${this.name}': ${errMsg}`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const voidAsyncDisposable = { | ||
| [Symbol.asyncDispose]: () => Promise.resolve(), | ||
| } as const satisfies AsyncDisposable; | ||
|
|
||
| const loadedScripts = new Set<string>(); | ||
|
|
||
| function createScriptSuffix(script: string): string { | ||
| // Import module with fragment so that reload works properly | ||
| // https://github.com/vim-denops/denops.vim/issues/227 | ||
| const suffix = loadedScripts.has(script) ? `#${performance.now()}` : ""; | ||
| loadedScripts.add(script); | ||
| return suffix; | ||
| } | ||
|
|
||
| /** NOTE: `emit()` is never throws or rejects. */ | ||
| async function emit(denops: Denops, name: string): Promise<void> { | ||
| try { | ||
| await denops.call("denops#_internal#event#emit", name); | ||
| } catch (e) { | ||
| console.error(`Failed to emit ${name}: ${e}`); | ||
| } | ||
| } | ||
|
|
||
| function resolveScriptUrl(script: string): string { | ||
| try { | ||
| return toFileUrl(script).href; | ||
| } catch { | ||
| return new URL(script, import.meta.url).href; | ||
| } | ||
| } | ||
|
|
||
| // See https://github.com/vim-denops/denops.vim/issues/358 for details | ||
| function isDenoCacheIssueError(e: unknown): boolean { | ||
| const expects = [ | ||
| "Could not find constraint in the list of versions: ", // Deno 1.40? | ||
| "Could not find version of ", // Deno 1.38 | ||
| ] as const; | ||
| if (e instanceof TypeError) { | ||
| return expects.some((expect) => e.message.startsWith(expect)); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| async function tryLoadImportMap( | ||
| script: string, | ||
| ): Promise<ImportMap | undefined> { | ||
| if (script.startsWith("http://") || script.startsWith("https://")) { | ||
| // We cannot load import maps for remote scripts | ||
| return undefined; | ||
| } | ||
| const PATTERNS = [ | ||
| "deno.json", | ||
| "deno.jsonc", | ||
| "import_map.json", | ||
| "import_map.jsonc", | ||
| ]; | ||
| // Convert file URL to path for file operations | ||
| const scriptPath = script.startsWith("file://") | ||
| ? fromFileUrl(new URL(script)) | ||
| : script; | ||
| const parentDir = dirname(scriptPath); | ||
| for (const pattern of PATTERNS) { | ||
| const importMapPath = join(parentDir, pattern); | ||
| try { | ||
| return await loadImportMap(importMapPath); | ||
| } catch (err: unknown) { | ||
| if (err instanceof Deno.errors.NotFound) { | ||
| // Ignore NotFound errors and try the next pattern | ||
| continue; | ||
| } | ||
| throw err; // Rethrow other errors | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| async function importPlugin(script: string): Promise<PluginModule> { | ||
| const suffix = createScriptSuffix(script); | ||
| const importMap = await tryLoadImportMap(script); | ||
| if (importMap) { | ||
| const importer = new ImportMapImporter(importMap); | ||
| return await importer.import<PluginModule>(`${script}${suffix}`); | ||
| } else { | ||
| return await import(`${script}${suffix}`); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix variable usage before declaration.
The static analysis tool detected that
voidAsyncDisposableis used before its declaration. While JavaScript hoisting might make this work at runtime, it's better to follow proper declaration order.Move the
voidAsyncDisposabledeclaration before thePluginclass:And remove the duplicate declaration at lines 104-106.
🧰 Tools
🪛 Biome (1.9.4)
[error] 20-20: This variable is used before its declaration.
The variable is declared here:
(lint/correctness/noInvalidUseBeforeDeclaration)
🤖 Prompt for AI Agents