Refactor flaggablePlugin with head/body structure and eliminate YAML/JSON parsing duplication#151
Refactor flaggablePlugin with head/body structure and eliminate YAML/JSON parsing duplication#151
Conversation
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
…ling Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
|
@copilot thanks. can we also have flaggableSpec have head anme/value pairs also? perhaps it needs to return a head/body type object |
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
Done in commit 07e4649. Added:
Example output: {
"spec": { "data": [1, 2, 3] },
"hasFlags": false,
"head": {
"format": "yaml",
"pluginName": "treebark",
"params": { "variableId": "myData" },
"wasDefaultId": false
}
} |
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
|
@copilot I think these plugins might use the new core functionality: css, dsvPlugin . but I'm not certain |
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
After analyzing both plugins: CSS Plugin: Already uses DSV Plugin: Doesn't use
Both plugins intentionally handle non-JSON/YAML content types, so the new DSV already uses |
|
@copilot this PR wasnt the win I was hoping for. It seems like a net gain of LOC when I thought it would equate to less code overall. |
You're right - the PR has a net gain of +136 LOC. Breaking it down: Removed duplication:
Added infrastructure:
The added complexity comes mainly from the head/body structure requested in earlier comments. Would you like me to:
A simpler approach would save ~80 LOC by keeping only the parseBody helper without the head metadata feature. |
|
@copilot My assumption was that some of the plugins would employ the shared code, thus reducing one-offs in several plugins. |
|
@copilot My assumption was that some of the plugins would employ the shared code, thus reducing one-offs in several plugins. |
You're right - the shared code IS being used by many plugins! Here's the actual impact: Plugins automatically benefiting from the refactored
These all use
Plugins with custom implementations:
The issue is that the 11 plugins already used Without the head metadata feature, we'd have roughly net -50 LOC. Do you want to keep the head structure or simplify? |
Multiple plugins (vega-lite, mermaid) were duplicating identical YAML/JSON parsing logic. Each manually detected format from fence info and parsed content inline. Additionally, extended the plugin architecture to include parsed fence info ("head") metadata alongside content ("body") in a structured format.
Changes
Created parsing helpers (
config.ts)parseBody- Centralizes YAML/JSON parsing with format detection, error handling, and null safetyparseHeadAndBody- Parses both fence info (head) and content (body) together, optimized to avoid double parsingconvertHeadToSerializable- Converts parsed head to JSON-serializable format with params as name/value pairsExtended RawFlaggableSpec (
factory.ts)headfield containing:format: 'json' | 'yaml'pluginName: stringparams: Record<string, string> (name/value pairs)wasDefaultId: booleanUpdated plugins
flaggablePlugin- Now usesparseHeadAndBodyand includes head metadata in all code pathsvega-lite- UsesparseHeadAndBody, eliminated 20 lines of duplicated parsingmermaid- UsesparseHeadAndBody, eliminated 25 lines of duplicated parsingImpact
11 plugins automatically benefit from the refactored
flaggablePlugin:These plugins use
flaggablePluginwithout custom fence implementations, so they automatically receive:Example Output
{ "spec": { "data": [1, 2, 3] }, "hasFlags": false, "head": { "format": "yaml", "pluginName": "treebark", "params": { "variableId": "myData" }, "wasDefaultId": false } }Code Changes
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.