From f10610a0a31b3de83169fd1204dffafc34463240 Mon Sep 17 00:00:00 2001 From: Kevin Hill Date: Wed, 5 Jun 2024 11:35:59 -0700 Subject: [PATCH 1/3] Initial commit of working plugin with settings toggle --- src/components/inputs/Codemirror.vue | 9 +- src/components/settings/SettingsEditorTab.vue | 15 ++ src/locales/en.json | 2 + .../CodeMirrorPluginKlipperDocsTooltips.ts | 143 ++++++++++++++++++ 4 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts diff --git a/src/components/inputs/Codemirror.vue b/src/components/inputs/Codemirror.vue index 9c54459ec..64fe110d4 100644 --- a/src/components/inputs/Codemirror.vue +++ b/src/components/inputs/Codemirror.vue @@ -16,6 +16,7 @@ import { vscodeDark } from '@uiw/codemirror-theme-vscode' import { StreamLanguage } from '@codemirror/language' import { klipper_config } from '@/plugins/StreamParserKlipperConfig' import { gcode } from '@/plugins/StreamParserGcode' +import { KlipperDocsTooltipPlugin } from "@/plugins/CodeMirrorPluginKlipperDocsTooltips" import { indentWithTab } from '@codemirror/commands' import { json } from '@codemirror/lang-json' import { css } from '@codemirror/lang-css' @@ -92,9 +93,11 @@ export default class Codemirror extends Mixins(BaseMixin) { if (this.$emit) { this.$emit('input', this.content) } - }), + }) ] + if (this.tooltipsPluginEnabled) extensions.push(KlipperDocsTooltipPlugin()) + if (['cfg', 'conf'].includes(this.fileExtension)) extensions.push(StreamLanguage.define(klipper_config)) else if (['gcode'].includes(this.fileExtension)) extensions.push(StreamLanguage.define(gcode)) else if (['json'].includes(this.fileExtension)) extensions.push(json()) @@ -110,5 +113,9 @@ export default class Codemirror extends Mixins(BaseMixin) { get tabSize() { return this.$store.state.gui.editor.tabSize || 2 } + + get tooltipsPluginEnabled() { + return this.$store.state.gui.editor.tooltipsEnabled || false + } } diff --git a/src/components/settings/SettingsEditorTab.vue b/src/components/settings/SettingsEditorTab.vue index f4b507eda..43afcb8b8 100644 --- a/src/components/settings/SettingsEditorTab.vue +++ b/src/components/settings/SettingsEditorTab.vue @@ -16,6 +16,13 @@ + + + + { + const { from, to, text } = view.state.doc.lineAt(pos) + + let start = pos, end = pos + while (start > from && /\w/.test(text[start - from - 1])) start-- + while (end < to && /\w/.test(text[end - from])) end++ + + const word = text.slice(start - from, end - from) + if (start === pos && side < 0 || end === pos && side > 0 || !CONFIG_SECTIONS.includes(word)) { + return null; + } + + return { + pos: start, + end, + above: true, + create() { + const div = document.createElement("div") + + Object.assign(div.style, { + padding: "3px 10px", + borderRadius: "5px", + backgroundColor: "#FFF", + }) + + div.innerHTML = `\ + + View documentation for [${word}] + `; + + return { dom: div } + } + } + }) +} From a32d6e4165186ce3846f3788579b20cf32511627 Mon Sep 17 00:00:00 2001 From: Kevin Hill Date: Wed, 5 Jun 2024 12:44:09 -0700 Subject: [PATCH 2/3] I forgot to run Prettier --- src/components/inputs/Codemirror.vue | 4 +- .../CodeMirrorPluginKlipperDocsTooltips.ts | 217 +++++++++--------- 2 files changed, 111 insertions(+), 110 deletions(-) diff --git a/src/components/inputs/Codemirror.vue b/src/components/inputs/Codemirror.vue index 64fe110d4..dfd81427e 100644 --- a/src/components/inputs/Codemirror.vue +++ b/src/components/inputs/Codemirror.vue @@ -16,7 +16,7 @@ import { vscodeDark } from '@uiw/codemirror-theme-vscode' import { StreamLanguage } from '@codemirror/language' import { klipper_config } from '@/plugins/StreamParserKlipperConfig' import { gcode } from '@/plugins/StreamParserGcode' -import { KlipperDocsTooltipPlugin } from "@/plugins/CodeMirrorPluginKlipperDocsTooltips" +import { KlipperDocsTooltipPlugin } from '@/plugins/CodeMirrorPluginKlipperDocsTooltips' import { indentWithTab } from '@codemirror/commands' import { json } from '@codemirror/lang-json' import { css } from '@codemirror/lang-css' @@ -93,7 +93,7 @@ export default class Codemirror extends Mixins(BaseMixin) { if (this.$emit) { this.$emit('input', this.content) } - }) + }), ] if (this.tooltipsPluginEnabled) extensions.push(KlipperDocsTooltipPlugin()) diff --git a/src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts b/src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts index 2fc6d27ae..f0aa443d7 100644 --- a/src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts +++ b/src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts @@ -1,121 +1,122 @@ /** * @link https://codemirror.net/examples/tooltip/ */ -import { hoverTooltip } from "@codemirror/view" +import { hoverTooltip } from '@codemirror/view' -const DOCUMENTATION_URL_BASE = "https://www.klipper3d.org/Config_Reference.html" +const DOCUMENTATION_URL_BASE = 'https://www.klipper3d.org/Config_Reference.html' const CONFIG_SECTIONS: readonly string[] = [ - "ad5206", - "adc_scaled", - "adc_temperature", - "adxl345", - "angle", - "axis_twist_compensation", - "bed_mesh", - "bed_screws", - "bed_tilt", - "bltouch", - "board_pins", - "controller_fan", - "delayed_gcode", - "display", - "display_data", - "display_glyph", - "display_template", - "dotstar", - "dual_carriage", - "duplicate_pin_override", - "endstop_phase", - "exclude_object", - "extruder", - "extruder1", - "extruder_stepper", - "fan", - "fan_generic", - "filament_motion_sensor", - "filament_switch_sensor", - "firmware_retraction", - "force_move", - "gcode_arcs", - "gcode_button", - "gcode_macro", - "hall_filament_width_sensor", - "heater_bed", - "heater_fan", - "heater_generic", - "homing_heaters", - "homing_override", - "idle_timeout", - "include", - "input_shaper", - "led", - "lis2dw", - "manual_stepper", - "mcp4018", - "mcp4451", - "mcp4728", - "mcu", - "menu", - "mpu9250", - "multi_pin", - "neopixel", - "output_pin", - "palette2", - "pause_resume", - "pca9533", - "pca9632", - "printer", - "probe", - "probe_eddy_current", - "pwm_cycle_time", - "pwm_tool", - "quad_gantry_level", - "replicape", - "resonance_tester", - "respond", - "safe_z_home", - "samd_sercom", - "save_variables", - "screws_tilt_adjust", - "sdcard_loop", - "servo", - "skew_correction", - "smart_effector", - "static_digital_output", - "stepper", - "stepper_x", - "stepper_y", - "stepper_z", - "stepper_z1", - "sx1509", - "temperature_fan", - "temperature_sensor", - "thermistor", - "tmc2130", - "tmc2208", - "tmc2209", - "tmc2240", - "tmc2660", - "tmc5160", - "tsl1401cl_filament_width_sensor", - "verify_heater", - "virtual_sdcard", - "z_thermal_adjust", - "z_tilt" + 'ad5206', + 'adc_scaled', + 'adc_temperature', + 'adxl345', + 'angle', + 'axis_twist_compensation', + 'bed_mesh', + 'bed_screws', + 'bed_tilt', + 'bltouch', + 'board_pins', + 'controller_fan', + 'delayed_gcode', + 'display', + 'display_data', + 'display_glyph', + 'display_template', + 'dotstar', + 'dual_carriage', + 'duplicate_pin_override', + 'endstop_phase', + 'exclude_object', + 'extruder', + 'extruder1', + 'extruder_stepper', + 'fan', + 'fan_generic', + 'filament_motion_sensor', + 'filament_switch_sensor', + 'firmware_retraction', + 'force_move', + 'gcode_arcs', + 'gcode_button', + 'gcode_macro', + 'hall_filament_width_sensor', + 'heater_bed', + 'heater_fan', + 'heater_generic', + 'homing_heaters', + 'homing_override', + 'idle_timeout', + 'include', + 'input_shaper', + 'led', + 'lis2dw', + 'manual_stepper', + 'mcp4018', + 'mcp4451', + 'mcp4728', + 'mcu', + 'menu', + 'mpu9250', + 'multi_pin', + 'neopixel', + 'output_pin', + 'palette2', + 'pause_resume', + 'pca9533', + 'pca9632', + 'printer', + 'probe', + 'probe_eddy_current', + 'pwm_cycle_time', + 'pwm_tool', + 'quad_gantry_level', + 'replicape', + 'resonance_tester', + 'respond', + 'safe_z_home', + 'samd_sercom', + 'save_variables', + 'screws_tilt_adjust', + 'sdcard_loop', + 'servo', + 'skew_correction', + 'smart_effector', + 'static_digital_output', + 'stepper', + 'stepper_x', + 'stepper_y', + 'stepper_z', + 'stepper_z1', + 'sx1509', + 'temperature_fan', + 'temperature_sensor', + 'thermistor', + 'tmc2130', + 'tmc2208', + 'tmc2209', + 'tmc2240', + 'tmc2660', + 'tmc5160', + 'tsl1401cl_filament_width_sensor', + 'verify_heater', + 'virtual_sdcard', + 'z_thermal_adjust', + 'z_tilt', ] export function KlipperDocsTooltipPlugin() { return hoverTooltip((view, pos, side) => { const { from, to, text } = view.state.doc.lineAt(pos) - let start = pos, end = pos + let start = pos, + end = pos while (start > from && /\w/.test(text[start - from - 1])) start-- while (end < to && /\w/.test(text[end - from])) end++ const word = text.slice(start - from, end - from) - if (start === pos && side < 0 || end === pos && side > 0 || !CONFIG_SECTIONS.includes(word)) { - return null; + if ((start === pos && side < 0) || (end === pos && side > 0) || !CONFIG_SECTIONS.includes(word)) { + return null } return { @@ -123,21 +124,21 @@ export function KlipperDocsTooltipPlugin() { end, above: true, create() { - const div = document.createElement("div") + const div = document.createElement('div') Object.assign(div.style, { - padding: "3px 10px", - borderRadius: "5px", - backgroundColor: "#FFF", + padding: '3px 10px', + borderRadius: '5px', + backgroundColor: '#FFF', }) div.innerHTML = `\ View documentation for [${word}] - `; + ` return { dom: div } - } + }, } }) } From 6aa6658ef992311f760c5d8e8570ed834fb857d6 Mon Sep 17 00:00:00 2001 From: Kevin Hill Date: Wed, 5 Jun 2024 14:41:26 -0700 Subject: [PATCH 3/3] removing keywords array. matching on words in brackets --- .../CodeMirrorPluginKlipperDocsTooltips.ts | 170 +++++------------- 1 file changed, 42 insertions(+), 128 deletions(-) diff --git a/src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts b/src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts index f0aa443d7..ca125bcbe 100644 --- a/src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts +++ b/src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts @@ -1,144 +1,58 @@ -/** - * @link https://codemirror.net/examples/tooltip/ - */ import { hoverTooltip } from '@codemirror/view' const DOCUMENTATION_URL_BASE = 'https://www.klipper3d.org/Config_Reference.html' -const CONFIG_SECTIONS: readonly string[] = [ - 'ad5206', - 'adc_scaled', - 'adc_temperature', - 'adxl345', - 'angle', - 'axis_twist_compensation', - 'bed_mesh', - 'bed_screws', - 'bed_tilt', - 'bltouch', - 'board_pins', - 'controller_fan', - 'delayed_gcode', - 'display', - 'display_data', - 'display_glyph', - 'display_template', - 'dotstar', - 'dual_carriage', - 'duplicate_pin_override', - 'endstop_phase', - 'exclude_object', - 'extruder', - 'extruder1', - 'extruder_stepper', - 'fan', - 'fan_generic', - 'filament_motion_sensor', - 'filament_switch_sensor', - 'firmware_retraction', - 'force_move', - 'gcode_arcs', - 'gcode_button', - 'gcode_macro', - 'hall_filament_width_sensor', - 'heater_bed', - 'heater_fan', - 'heater_generic', - 'homing_heaters', - 'homing_override', - 'idle_timeout', - 'include', - 'input_shaper', - 'led', - 'lis2dw', - 'manual_stepper', - 'mcp4018', - 'mcp4451', - 'mcp4728', - 'mcu', - 'menu', - 'mpu9250', - 'multi_pin', - 'neopixel', - 'output_pin', - 'palette2', - 'pause_resume', - 'pca9533', - 'pca9632', - 'printer', - 'probe', - 'probe_eddy_current', - 'pwm_cycle_time', - 'pwm_tool', - 'quad_gantry_level', - 'replicape', - 'resonance_tester', - 'respond', - 'safe_z_home', - 'samd_sercom', - 'save_variables', - 'screws_tilt_adjust', - 'sdcard_loop', - 'servo', - 'skew_correction', - 'smart_effector', - 'static_digital_output', - 'stepper', - 'stepper_x', - 'stepper_y', - 'stepper_z', - 'stepper_z1', - 'sx1509', - 'temperature_fan', - 'temperature_sensor', - 'thermistor', - 'tmc2130', - 'tmc2208', - 'tmc2209', - 'tmc2240', - 'tmc2660', - 'tmc5160', - 'tsl1401cl_filament_width_sensor', - 'verify_heater', - 'virtual_sdcard', - 'z_thermal_adjust', - 'z_tilt', -] - +/** + * @link https://codemirror.net/examples/tooltip/ + */ export function KlipperDocsTooltipPlugin() { - return hoverTooltip((view, pos, side) => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + return hoverTooltip((view, pos, _side) => { const { from, to, text } = view.state.doc.lineAt(pos) let start = pos, end = pos - while (start > from && /\w/.test(text[start - from - 1])) start-- - while (end < to && /\w/.test(text[end - from])) end++ - const word = text.slice(start - from, end - from) - if ((start === pos && side < 0) || (end === pos && side > 0) || !CONFIG_SECTIONS.includes(word)) { + while (start > from && text[start - from - 1] !== '[') start-- + while (end < to && text[end - from] !== ']') end++ + + if (text[start - from - 1] !== '[' || text[end - from] !== ']') { return null } - return { - pos: start, - end, - above: true, - create() { - const div = document.createElement('div') - - Object.assign(div.style, { - padding: '3px 10px', - borderRadius: '5px', - backgroundColor: '#FFF', - }) - - div.innerHTML = `\ - - View documentation for [${word}] - ` - - return { dom: div } - }, + const content = text.slice(start - from, end - from).trim() + const words = content.split(/\s+/) + + for (const word of words) { + const wordStart = text.indexOf(word, start - from) + from + const wordEnd = wordStart + word.length + + if (pos >= wordStart && pos <= wordEnd) { + return { + pos: wordStart, + end: wordEnd, + above: true, + create() { + const div = document.createElement('div') + + Object.assign(div.style, { + padding: '3px 10px', + borderRadius: '5px', + color: "#000", + backgroundColor: '#FFF', + }) + + div.innerHTML = `\ + View + ${word} + documentation` + + return { dom: div } + }, + } + } } + + return null }) }