-
Notifications
You must be signed in to change notification settings - Fork 254
Auto-load context and stylesheet for diagnostics #70
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
Auto-load context and stylesheet for diagnostics #70
Conversation
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.
Pull Request Overview
This PR enhances the POML Language Server to automatically load context and stylesheet files when no preview options exist, improving the default validation behavior for POML documents.
- Auto-loads associated
.context.jsonand.stylesheet.jsonfiles based on the POML file's base name - Replaces inline default options creation with a dedicated method for better maintainability
- Improves diagnostics by using auto-discovered context and stylesheet files
| import { PomlFile, PomlToken } from 'poml/file'; | ||
| import { encodingForModel, Tiktoken } from 'js-tiktoken'; | ||
| import { readFile } from 'fs/promises'; | ||
| import * as fs from 'fs'; |
Copilot
AI
Aug 4, 2025
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.
Consider using 'fs/promises' for consistency with the existing 'readFile' import and to avoid blocking synchronous file operations. Replace 'fs.existsSync' with 'fs.promises.access' or similar async alternatives.
| import * as fs from 'fs'; | |
| import * as fs from 'fs/promises'; |
| private getAssociatedOptions(uri: string): WebviewUserOptions { | ||
| let options = this.associatedOptions.get(uri); | ||
| if (!options) { | ||
| const filePath = fileURLToPath(uri); | ||
| const base = filePath.replace(/(\.source)?\.poml$/i, ''); | ||
| const contexts: string[] = []; | ||
| const stylesheets: string[] = []; | ||
| if (fs.existsSync(`${base}.context.json`)) { | ||
| contexts.push(`${base}.context.json`); | ||
| } | ||
| if (fs.existsSync(`${base}.stylesheet.json`)) { | ||
| stylesheets.push(`${base}.stylesheet.json`); |
Copilot
AI
Aug 4, 2025
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.
Using synchronous file system operations can block the event loop in a language server. Consider using async file operations with try-catch blocks or fs.promises.access() to avoid blocking operations.
| private getAssociatedOptions(uri: string): WebviewUserOptions { | |
| let options = this.associatedOptions.get(uri); | |
| if (!options) { | |
| const filePath = fileURLToPath(uri); | |
| const base = filePath.replace(/(\.source)?\.poml$/i, ''); | |
| const contexts: string[] = []; | |
| const stylesheets: string[] = []; | |
| if (fs.existsSync(`${base}.context.json`)) { | |
| contexts.push(`${base}.context.json`); | |
| } | |
| if (fs.existsSync(`${base}.stylesheet.json`)) { | |
| stylesheets.push(`${base}.stylesheet.json`); | |
| private async getAssociatedOptions(uri: string): Promise<WebviewUserOptions> { | |
| let options = this.associatedOptions.get(uri); | |
| if (!options) { | |
| const filePath = fileURLToPath(uri); | |
| const base = filePath.replace(/(\.source)?\.poml$/i, ''); | |
| const contexts: string[] = []; | |
| const stylesheets: string[] = []; | |
| try { | |
| await fs.promises.access(`${base}.context.json`, fs.constants.F_OK); | |
| contexts.push(`${base}.context.json`); | |
| } catch (e) { | |
| // file does not exist, do nothing | |
| } | |
| try { | |
| await fs.promises.access(`${base}.stylesheet.json`, fs.constants.F_OK); | |
| stylesheets.push(`${base}.stylesheet.json`); | |
| } catch (e) { | |
| // file does not exist, do nothing |
| if (fs.existsSync(`${base}.context.json`)) { | ||
| contexts.push(`${base}.context.json`); | ||
| } | ||
| if (fs.existsSync(`${base}.stylesheet.json`)) { |
Copilot
AI
Aug 4, 2025
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.
Using synchronous file system operations can block the event loop in a language server. Consider using async file operations with try-catch blocks or fs.promises.access() to avoid blocking operations.
Summary
Testing
npm run build-webviewnpm run build-clinpm run lintnpm testpython -m pytest python/testshttps://chatgpt.com/codex/tasks/task_e_688ce507d084832e9c2577dcb9dca818