-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add dynamic intellisense for parentId/id values in YAML editor #72
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
lostb1t
merged 3 commits into
streamyfin:main
from
kamilkosek:feat/monaco-completion-provider
Oct 8, 2025
Merged
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,118 @@ export default function (view, params) { | |||||
| const Page = { | ||||||
| editor: null, | ||||||
| yaml: null, | ||||||
| parentIdProvider: null, | ||||||
| parentIdSuggestions: null, | ||||||
| // Fetch libraries and collections from Jellyfin and map to Monaco suggestions | ||||||
| loadParentIdSuggestions: async function () { | ||||||
| try { | ||||||
| const userId = await window.ApiClient.getCurrentUserId?.() ?? null; | ||||||
|
|
||||||
| // Build URLs using ApiClient to preserve base path and auth | ||||||
| // Prefer user views over raw media folders for broader compatibility | ||||||
| const libsUrl = userId | ||||||
| ? window.ApiClient.getUrl(`Users/${userId}/Views`) | ||||||
| : window.ApiClient.getUrl('Library/MediaFolders'); | ||||||
| const collectionsUrl = userId | ||||||
| ? window.ApiClient.getUrl(`Users/${userId}/Items`, { | ||||||
| IncludeItemTypes: 'BoxSet', | ||||||
| Recursive: true, | ||||||
| SortBy: 'SortName', | ||||||
| SortOrder: 'Ascending' | ||||||
| }) | ||||||
| : null; | ||||||
|
|
||||||
| // Fetch in parallel using ApiClient.ajax to include auth | ||||||
| const [libsRes, colRes] = await Promise.all([ | ||||||
| window.ApiClient.ajax({ type: 'GET', url: libsUrl, contentType: 'application/json' }), | ||||||
| collectionsUrl | ||||||
| ? window.ApiClient.ajax({ type: 'GET', url: collectionsUrl, contentType: 'application/json' }) | ||||||
| : Promise.resolve(null) | ||||||
| ]); | ||||||
|
|
||||||
| const libsJson = libsRes ? libsRes : { Items: [] }; | ||||||
| const colsJson = colRes ? colRes : { Items: [] }; | ||||||
|
|
||||||
| // Normalize arrays (Jellyfin usually returns { Items: [...] }) | ||||||
| const libraries = Array.isArray(libsJson?.Items) ? libsJson.Items : (Array.isArray(libsJson) ? libsJson : []); | ||||||
| const collections = Array.isArray(colsJson?.Items) ? colsJson.Items : (Array.isArray(colsJson) ? colsJson : []); | ||||||
|
|
||||||
| const libSuggestions = libraries | ||||||
| .filter(i => i?.Id && i?.Name) | ||||||
| .map(i => ({ | ||||||
| label: `${i.Name} (${i.Id})`, | ||||||
| kind: monaco.languages.CompletionItemKind.Value, | ||||||
| insertText: i.Id, | ||||||
| detail: 'Library folder', | ||||||
| documentation: i.Path ? `Path: ${i.Path}` : undefined | ||||||
| })); | ||||||
|
|
||||||
| const colSuggestions = collections | ||||||
| .filter(i => i?.Id && i?.Name) | ||||||
| .map(i => ({ | ||||||
| label: `${i.Name} (${i.Id})`, | ||||||
| kind: monaco.languages.CompletionItemKind.Value, | ||||||
| insertText: i.Id, | ||||||
| detail: 'Collection', | ||||||
| documentation: i.Overview || undefined | ||||||
| })); | ||||||
|
|
||||||
| Page.parentIdSuggestions = [...libSuggestions, ...colSuggestions]; | ||||||
| } catch (e) { | ||||||
| console.warn('Failed to load parentId suggestions', e); | ||||||
| Page.parentIdSuggestions = []; | ||||||
| } | ||||||
| }, | ||||||
| // Register a YAML completion provider that triggers when value for key 'parentId' is being edited | ||||||
| registerParentIdProvider: function () { | ||||||
| if (Page.parentIdProvider) return; // avoid duplicates | ||||||
|
|
||||||
| Page.parentIdProvider = monaco.languages.registerCompletionItemProvider('yaml', { | ||||||
| triggerCharacters: [':', ' ', '-', '\n', '"', "'"], | ||||||
| provideCompletionItems: async (model, position) => { | ||||||
| try { | ||||||
| const line = model.getLineContent(position.lineNumber); | ||||||
| const beforeCursor = line.substring(0, position.column - 1); | ||||||
| // Heuristic: we're in a value position for a key named 'parentId' | ||||||
| // Match lines like: "parentId: |" or "id: |" with optional indent or list dash | ||||||
| const isTargetLine = /(^|\s|-)\b(parentId|id)\b\s*:\s*[^#]*$/i.test(beforeCursor); | ||||||
lostb1t marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| if (!isTargetLine) { | ||||||
| return { suggestions: [] }; | ||||||
| } | ||||||
|
|
||||||
| if (!Array.isArray(Page.parentIdSuggestions)) { | ||||||
| await Page.loadParentIdSuggestions(); | ||||||
| } | ||||||
|
|
||||||
| // Compute replacement range: from word start to cursor | ||||||
| const word = model.getWordUntilPosition(position); | ||||||
| const startColFromColon = (() => { | ||||||
| const idx = beforeCursor.lastIndexOf(':'); | ||||||
| if (idx === -1) return word.startColumn; | ||||||
| let start = idx + 1; // first char after colon | ||||||
| // skip spaces | ||||||
| while (start < beforeCursor.length && beforeCursor.charAt(start) === ' ') start++; | ||||||
| // skip optional opening quotes | ||||||
| while (start < beforeCursor.length && (beforeCursor.charAt(start) === '"' || beforeCursor.charAt(start) === "'")) start++; | ||||||
| // Monaco columns are 1-based | ||||||
| return start + 1; | ||||||
| })(); | ||||||
lostb1t marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| const range = new monaco.Range( | ||||||
| position.lineNumber, | ||||||
| Math.max(1, startColFromColon), | ||||||
| position.lineNumber, | ||||||
| position.column | ||||||
| ); | ||||||
|
|
||||||
| const suggestions = Page.parentIdSuggestions.map(s => ({ ...s, range })); | ||||||
| return { suggestions }; | ||||||
| } catch (err) { | ||||||
| console.warn('parentId provider error', err); | ||||||
| return { suggestions: [] }; | ||||||
| } | ||||||
| } | ||||||
| }); | ||||||
| }, | ||||||
| saveConfig: function (e) { | ||||||
| e.preventDefault(); | ||||||
| shared.setYamlConfig(Page.editor.getModel().getValue()) | ||||||
|
|
@@ -76,6 +188,9 @@ export default function (view, params) { | |||||
| saveBtn().addEventListener("click", Page.saveConfig); | ||||||
| exampleBtn().addEventListener("click", Page.resetConfig); | ||||||
|
|
||||||
| // Register dynamic intellisense for parentId values | ||||||
| Page.registerParentIdProvider(); | ||||||
|
|
||||||
| if (shared.getConfig() && Page.editor == null) { | ||||||
| Page.loadConfig(shared.getConfig()); | ||||||
| } | ||||||
|
|
@@ -102,8 +217,10 @@ export default function (view, params) { | |||||
| console.log("Hiding") | ||||||
| Page?.editor?.dispose() | ||||||
| Page?.yaml?.dispose() | ||||||
| Page?.parentIdProvider?.dispose?.() | ||||||
|
||||||
| Page?.parentIdProvider?.dispose?.() | |
| if (typeof Page?.parentIdProvider?.dispose === 'function') Page.parentIdProvider.dispose(); |
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.
Make
await ??precedence explicit and allow retries after load failureawait … ?? nullto avoid precedence gotchas.parentIdSuggestions = []. That prevents future retries in the same session because the provider checks only for Array-ness. Useundefinedto re-attempt later.Also applies to: 80-83
🤖 Prompt for AI Agents