Skip to content
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

feat: CodeMirror plugin for Klipper Documentation tooltip links in editor #1898

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
I forgot to run Prettier
  • Loading branch information
khill-fbmc committed Jun 5, 2024
commit a32d6e4165186ce3846f3788579b20cf32511627
4 changes: 2 additions & 2 deletions src/components/inputs/Codemirror.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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())
Expand Down
217 changes: 109 additions & 108 deletions src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts
Original file line number Diff line number Diff line change
@@ -1,143 +1,144 @@
/**
* @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 {
pos: start,
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 = `\
<a style="text-decoration: none;" href="${DOCUMENTATION_URL_BASE}#${word}" rel="noopener" target="_blank">
View documentation for [${word}]
</a>`;
</a>`

return { dom: div }
}
},
}
})
}
Loading