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: adds Codelens and code folding support #665

Merged
merged 10 commits into from
May 10, 2022
108 changes: 108 additions & 0 deletions src/components/widgets/filesystem/setupMonaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
import { loadWASM } from 'onigasm'
import { IGrammarDefinition, Registry } from 'monaco-textmate'
import { wireTmGrammars } from 'monaco-editor-textmate'
import getVueApp from '@/util/get-vue-app'
import themeDark from '@/monaco/theme/editor.dark.theme.json'
import themeLight from '@/monaco/theme/editor.light.theme.json'

const extruderRegExp = /^extruder[0-9]+$/

const getDocsSection = (sectionName: string) => {
if (sectionName.startsWith('stepper_')) {
return 'stepper'
}

if (extruderRegExp.test(sectionName)) {
return 'extruder'
}

return sectionName
}

async function setupMonaco () {
const wasm = await require('onigasm/lib/onigasm.wasm')
await loadWASM(wasm.default)
Expand Down Expand Up @@ -49,6 +64,99 @@ async function setupMonaco () {
}
})

const app = getVueApp()

monaco.editor.registerCommand('fluidd_open_docs', (_, isMoonrakerConfig, hash) => {
if (isMoonrakerConfig) {
const url = app.$t('app.file_system.url.moonraker_config', { hash }).toString()
window.open(url)
} else {
const url = app.$t('app.file_system.url.klipper_config', { hash }).toString()
window.open(url)
}
})

monaco.languages.registerCodeLensProvider('klipper-config', {
provideCodeLenses: (model) => {
const isMoonrakerConfig = model.uri.path.toLowerCase().endsWith('/moonraker.conf')

const linesContent = model.getLinesContent()

const sections = linesContent.reduce((ranges, lineContent, index) => {
const section = /^\[([^\]]+)\]/.exec(lineContent)
if (section) {
const [sectionName] = section[1].split(' ')

const referenceSection = getDocsSection(sectionName)

return ranges.concat({
referenceSection,
range: {
startLineNumber: index + 1,
startColumn: model.getLineFirstNonWhitespaceColumn(index + 1),
endLineNumber: index + 1,
endColumn: model.getLineLastNonWhitespaceColumn(index + 1)
}
})
}
return ranges
}, [] as { referenceSection: string, range: monaco.IRange }[])

return {
lenses: sections.map((section, index) =>
({
range: section.range,
id: `docs${index}`,
command: {
id: 'fluidd_open_docs',
title: app.$t('app.file_system.label.view_section_documentation', { section: section.referenceSection }).toString(),
arguments: [isMoonrakerConfig, section.referenceSection]
}
})
),
dispose: () => undefined
}
},
resolveCodeLens: (_model, codeLens) => codeLens
})

monaco.languages.registerFoldingRangeProvider('klipper-config', {
provideFoldingRanges: (model) => {
const linesContent = model.getLinesContent()

return linesContent.reduce((sections, lineContent, index) => {
const isSection = /^\[([^\]]+)\]/.test(lineContent)

if (isSection) {
return sections.concat({
start: index + 1,
end: index + 1,
kind: monaco.languages.FoldingRangeKind.Region
})
}

const lastSection = sections.length > 0 ? sections[sections.length - 1] : undefined
const isLastSectionComment = lastSection?.kind === monaco.languages.FoldingRangeKind.Comment

const isComment = lineContent.startsWith('#')

if (isComment && !isLastSectionComment) {
return sections.concat({
start: index + 1,
end: index + 1,
kind: monaco.languages.FoldingRangeKind.Comment
})
}

if (lineContent.trim().length > 0 && isComment === isLastSectionComment) {
sections[sections.length - 1].end = index + 1
}

return sections
}, [] as monaco.languages.FoldingRange[])
}
})

// Defined the themes.
monaco.editor.defineTheme('dark-converted', themeDark as any)
monaco.editor.defineTheme('light-converted', themeLight as any)
Expand Down
2 changes: 2 additions & 0 deletions src/locales/cn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ app:
tooltip:
low_on_space: 磁盘空间不足
root_disabled: '{root} G代码文件目录不可用,请首先连接打印机或者检查G代码存放目录的路径与读权限'
url:
klipper_config: 'https://www.klipper3d.org/zh/Config_Reference.html#%{hash}'
gcode:
btn:
load_current_file: 载入当前文件
Expand Down
2 changes: 2 additions & 0 deletions src/locales/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ app:
file_name: Dateiname
transfer_rate: Transferrate
uploaded: Hochgeladen
view_section_documentation: >-
Dokumentation für '%{section}' anzeigen
msg:
confirm: Sind Sie sicher? Es werden alle enthaltenen Dateien und Ordner gelöscht.
not_found: Keine Dateien gefunden
Expand Down
5 changes: 5 additions & 0 deletions src/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ app:
file_name: Filename
transfer_rate: Transfer rate
uploaded: Uploaded
view_section_documentation: >-
View '%{section}' documentation
msg:
confirm: Are you sure? This will delete all files and folders.
not_found: No files found
Expand All @@ -99,6 +101,9 @@ app:
tooltip:
low_on_space: Low on disk space
root_disabled: '{root} root is not available. Please check your logs.'
url:
klipper_config: 'https://www.klipper3d.org/Config_Reference.html#%{hash}'
moonraker_config: 'https://moonraker.readthedocs.io/en/latest/configuration/#%{hash}'
gcode:
btn:
load_current_file: Load Current File
Expand Down
10 changes: 5 additions & 5 deletions src/monaco/language/klipper-config.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@
},
"known_config_block_name": {
"name": "storage.type",
"match": "(?i)\\b(ad5206|adc_temperature|bed_mesh|bed_screws|bed_tilt|bltouch|board_pins|controller_fan|delayed_gcode|delta_calibrate|display|display_data|display_template|dotstar|dual_carriage|endstop_phase|extruder_stepper|extruder[1-9]{0,1}|fan|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|manual_stepper|mcp4018|mcp4451|mcp4728|mcu|menu|multi_pin|neopixel|output_pin|pause_resume|printer|probe|quad_gantry_level|replicape|respond|safe_z_home|samd_sercom|screws_tilt_adjust|servo|skew_correction|static_digital_output|stepper_(bed|arm|[abcdxy]|z[1-9]{0,1})|sx1509|temperature_fan|temperature_sensor|thermistor|tsl1401cl_filament_width_sensor|verify_heater|virtual_sdcard|z_tilt)\\b"
"match": "(?i)\\b(ad5206|adc_scaled|adc_temperature|adxl345|angle|bed_mesh|bed_screws|bed_tilt|bltouch|board_pins|controller_fan|delayed_gcode|delta_calibrate|display|display_data|display_glyph|display_status|display_template|dotstar|dual_carriage|duplicate_pin_override|endstop_phase|extruder_stepper|extruder[1-9]{0,1}|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|manual_probe|manual_stepper|mcp4018|mcp4451|mcp4728|mcu|menu|multi_pin|neopixel|output_pin|palette2|pause_resume|pca9533|pca9632|printer|probe|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_(bed|arm|[abcdxy]|z[1-9]{0,1})|sx1509|temperature_fan|temperature_sensor|thermistor|tsl1401cl_filament_width_sensor|verify_heater|virtual_sdcard|z_tilt)\\b"
},
"known_driver_type": {
"name": "support.type",
"match": "(?i)\\btmc(2130|2208|2209|2660|5160)\\b"
},
"known_thermistor_type": {
"name": "support.type",
"match": "(?i)\\b(EPCOS 100K B57560G104F|ATC Semitec 104GT-2|NTC 100K beta 3950|Honeywell 100K 135-104LAG-J01|NTC 100K MGB18-104F39050L32)\\b"
"match": "(?i)\\b(EPCOS 100K B57560G104F|ATC Semitec 104GT-2|ATC Semitec 104NT-4-R025H42G|Generic 3950|NTC 100K beta 3950|Honeywell 100K 135-104LAG-J01|NTC 100K MGB18-104F39050L32|SliceEngineering 450|TDK NTCG104LH104JT1)\\b"
},
"known_extruder_sensor_type": {
"name": "support.type",
"match": "(?i)\\b(MAX6675|MAX31855|MAX31856|MAX31865|PT100 INA826|AD595|AD8494|AD8495|AD8496|AD8497|PT1000|BME280)\\b"
"match": "(?i)\\b(MAX6675|MAX31855|MAX31856|MAX31865|PT100 INA826|AD595|AD597|AD8494|AD8495|AD8496|AD8497|PT1000|BMP280|BME280|BME680|HTU21D|SI7013|SI7020|SI7021|SHT21|LM75|temperature_mcu|temperature_host|DS18B20)\\b"
},
"known_control_type": {
"name": "support.type",
Expand Down Expand Up @@ -196,7 +196,7 @@
},
"gcode_extended_command": {
"name": "keyword.operator",
"match": "(?i)^\\s*(ABORT|ACCEPT|ACTIVATE_EXTRUDER|BED_MESH_CALIBRATE|BED_MESH_CLEAR|BED_MESH_MAP|BED_MESH_OUTPUT|BED_MESH_PROFILE|BED_SCREWS_ADJUST|BED_TILT_CALIBRATE|BLTOUCH_DEBUG|BLTOUCH_STORE|CALC_MEASURED_SKEW|CLEAR_PAUSE|DELTA_ANALYZE|DELTA_CALIBRATE|DUMP_TMC|ENDSTOP_PHASE_CALIBRATE|FIRMWARE_RESTART|FORCE_MOVE|GET_CURRENT_SKEW|GET_POSITION|GET_RETRACTION|HELP|MANUAL_PROBE|MANUAL_STEPPER|PAUSE|PID_CALIBRATE|PROBE|PROBE_ACCURACY|PROBE_CALIBRATE|QUAD_GANTRY_LEVEL|QUERY_ADC|QUERY_ENDSTOPS|QUERY_FILAMENT_SENSOR|QUERY_PROBE|RESPOND|RESTART|RESTORE_GCODE_STATE|RESUME|SAVE_CONFIG|SAVE_GCODE_STATE|SCREWS_TILT_CALCULATE|SET_DUAL_CARRIAGE|SET_EXTRUDER_STEP_DISTANCE|SET_FILAMENT_SENSOR|SET_GCODE_OFFSET|SET_GCODE_VARIABLE|SET_HEATER_TEMPERATURE|SET_IDLE_TIMEOUT|SET_KINEMATIC_POSITION|SET_LED|SET_PIN|SET_PRESSURE_ADVANCE|SET_RETRACTION|SET_SERVO|SET_SKEW|SET_STEPPER_ENABLE|SET_TMC_CURRENT|SET_TMC_FIELD|SET_VELOCITY_LIMIT|SKEW_PROFILE|STATUS|STEPPER_BUZZ|TESTZ|TUNING_TOWER|TURN_OFF_HEATERS|UPDATE_DELAYED_GCODE|Z_ENDSTOP_CALIBRATE|Z_TILT_ADJUST)\\s"
"match": "(?i)^\\s*(ABORT|ACCELEROMETER_DEBUG_READ|ACCELEROMETER_DEBUG_WRITE|ACCELEROMETER_MEASURE|ACCELEROMETER_QUERY|ACCEPT|ACTIVATE_EXTRUDER|ANGLE_CALIBRATE|ANGLE_DEBUG_READ|ANGLE_DEBUG_WRITE|BED_MESH_CALIBRATE|BED_MESH_CLEAR|BED_MESH_MAP|BED_MESH_OFFSET|BED_MESH_OUTPUT|BED_MESH_PROFILE|BED_SCREWS_ADJUST|BED_TILT_CALIBRATE|BLTOUCH_DEBUG|BLTOUCH_STORE|CALC_MEASURED_SKEW|CANCEL_PRINT|CLEAR_PAUSE|DELTA_ANALYZE|DELTA_CALIBRATE|DISABLE_FILAMENT_WIDTH_LOG|DISABLE_FILAMENT_WIDTH_SENSOR|DUMP_TMC|ENABLE_FILAMENT_WIDTH_LOG|ENABLE_FILAMENT_WIDTH_SENSOR|ENDSTOP_PHASE_CALIBRATE|FIRMWARE_RESTART|FORCE_MOVE|GET_CURRENT_SKEW|GET_POSITION|GET_RETRACTION|HELP|INIT_TMC|MANUAL_PROBE|MANUAL_STEPPER|MEASURE_AXES_NOISE|PALETTE_CLEAR|PALETTE_CONNECT|PALETTE_CUT|PALETTE_DISCONNECT|PALETTE_SMART_LOAD|PAUSE|PID_CALIBRATE|PROBE|PROBE_ACCURACY|PROBE_CALIBRATE|QUAD_GANTRY_LEVEL|QUERY_ADC|QUERY_ENDSTOPS|QUERY_FILAMENT_SENSOR|QUERY_FILAMENT_WIDTH|QUERY_PROBE|QUERY_RAW_FILAMENT_WIDTH|RESET_FILAMENT_WIDTH_SENSOR|RESET_SMART_EFFECTOR|RESPOND|RESTART|RESTORE_GCODE_STATE|RESUME|SAVE_CONFIG|SAVE_GCODE_STATE|SAVE_VARIABLE|SCREWS_TILT_CALCULATE|SDCARD_LOOP_BEGIN|SDCARD_LOOP_DESIST|SDCARD_LOOP_END|SDCARD_PRINT_FILE|SDCARD_RESET_FILE|SET_DISPLAY_GROUP|SET_DUAL_CARRIAGE|SET_EXTRUDER_ROTATION_DISTANCE|SET_EXTRUDER_STEP_DISTANCE|SET_FAN_SPEED|SET_FILAMENT_SENSOR|SET_GCODE_OFFSET|SET_GCODE_VARIABLE|SET_HEATER_TEMPERATURE|SET_IDLE_TIMEOUT|SET_INPUT_SHAPER|SET_KINEMATIC_POSITION|SET_LED|SET_LED_TEMPLATE|SET_PIN|SET_PRESSURE_ADVANCE|SET_RETRACTION|SET_SERVO|SET_SKEW|SET_SMART_EFFECTOR|SET_STEPPER_ENABLE|SET_TEMPERATURE_FAN_TARGET|SET_TMC_CURRENT|SET_TMC_FIELD|SET_VELOCITY_LIMIT|SHAPER_CALIBRATE|SKEW_PROFILE|STATUS|STEPPER_BUZZ|SYNC_EXTRUDER_MOTION|SYNC_STEPPER_TO_EXTRUDER|TEMPERATURE_WAIT|TEST_RESONANCES|TESTZ|TUNING_TOWER|TURN_OFF_HEATERS|UPDATE_DELAYED_GCODE|Z_ENDSTOP_CALIBRATE|Z_OFFSET_APPLY_ENDSTOP|Z_OFFSET_APPLY_PROBE|Z_TILT_ADJUST)\\s"
},
"gcode_extended_parameter": {
"begin": "(?i)\\b(AC|ACCEL|ACCEL_TO_DECEL|AD|ADVANCE|ANGLE|BAND|BD|BLUE|CARRIAGE|CLEAR|COMMAND|CURRENT|DISTANCE|DURATION|ENABLE|EXTRUDER|FACTOR|FIELD|GREEN|HEATER|HOLDCURRENT|ID|INDEX|LED|LIFT_SPEED|LOAD|MACRO|METHOD|MODE|MOVE_SPEED|MSG|NAME|PARAMETER|PGP|PIN|PREFIX|PROBE_SPEED|PULLUP|RED|REMOVE|RETRACT_LENGTH|RETRACT_SPEED|SAMPLE_RETRACT_DIST|SAMPLES|SAMPLES_RESULT|SAMPLES_TOLERANCE|SAMPLES_TOLERANCE_RETRIES|SAVE|SENSOR|SERVO|SET_POSITION|SMOOTH_TIME|SPEED|SQUARE_CORNER_VELOCITY|START|STEPPER|STOP_ON_ENDSTOP|SYNC|TARGET|TIMEOUT|TRANSMIT|TYPE|UNRETRACT_EXTRA_LENGTH|UNRETRACT_SPEED|VALUE|VARIABLE|VELOCITY|WIDTH|WRITE_FILE|X|X_ADJUST|XY|XZ|Y|Y_ADJUST|YZ|Z|Z_ADJUST)=",
Expand Down Expand Up @@ -226,4 +226,4 @@
}
},
"scopeName": "source.klipper-config"
}
}
5 changes: 5 additions & 0 deletions src/util/get-vue-app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getVueApp = () => {
return (document.querySelector('#app') as any).__vue__
}

export default getVueApp
3 changes: 0 additions & 3 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ module.exports = defineConfig({
languages: ['markdown'],
features: [
'!codeAction',
'!codelens',
'!colorPicker',
'!contextmenu',
'!folding',
'!hover',
'!gotoError',
'!gotoLine',
'!gotoSymbol',
'!quickCommand',
'!quickHelp',
'!referenceSearch',
'!snippet'
Expand Down