Auto-load context and stylesheet for diagnostics#70
Conversation
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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`); |
There was a problem hiding this comment.
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`)) { |
There was a problem hiding this comment.
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