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 all commits
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
7 changes: 7 additions & 0 deletions src/components/inputs/Codemirror.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -95,6 +96,8 @@ export default class Codemirror extends Mixins(BaseMixin) {
}),
]

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())
Expand All @@ -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
}
}
</script>
15 changes: 15 additions & 0 deletions src/components/settings/SettingsEditorTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
<v-switch v-model="confirmUnsavedChanges" hide-details class="mt-0" />
</settings-row>
<v-divider class="my-2" />
<settings-row
:title="$t('Settings.EditorTab.ToggleDocumentationTooltips')"
:sub-title="$t('Settings.EditorTab.ToggleDocumentationTooltipsDescription')"
:dynamic-slot-width="true">
<v-switch v-model="toggleTooltips" hide-details class="mt-0" />
</settings-row>
<v-divider class="my-2" />
<settings-row
:title="$t('Settings.EditorTab.TabSize')"
:sub-title="$t('Settings.EditorTab.TabSizeDescription')"
Expand Down Expand Up @@ -75,6 +82,14 @@ export default class SettingsEditorTab extends Mixins(BaseMixin) {
this.$store.dispatch('gui/saveSetting', { name: 'editor.escToClose', value: newVal })
}

get toggleTooltips() {
return this.$store.state.gui.editor.tooltipsEnabled
}

set toggleTooltips(newVal) {
this.$store.dispatch('gui/saveSetting', { name: 'editor.tooltipsEnabled', value: newVal })
}

get confirmUnsavedChanges() {
return this.$store.state.gui.editor.confirmUnsavedChanges
}
Expand Down
2 changes: 2 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,8 @@
"Spaces": "Spaces: {count}",
"TabSize": "TAB Size",
"TabSizeDescription": "Adjusts how many spaces should be indented for TAB",
"ToggleDocumentationTooltips": "Enable tooltips to Klipper Docs",
"ToggleDocumentationTooltipsDescription": "When enabled, you can hover over config section headers and get a link to the documentation.",
"UseEscToClose": "Use ESC to close editor",
"UseEscToCloseDescription": "Allows the ESC key to close the editor"
},
Expand Down
58 changes: 58 additions & 0 deletions src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { hoverTooltip } from '@codemirror/view'

Check failure on line 1 in src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts

View workflow job for this annotation

GitHub Actions / Prettier

src/plugins/CodeMirrorPluginKlipperDocsTooltips.ts#L1

There are issues with this file's formatting, please run Prettier to fix the errors

const DOCUMENTATION_URL_BASE = 'https://www.klipper3d.org/Config_Reference.html'

/**
* @link https://codemirror.net/examples/tooltip/
*/
export function KlipperDocsTooltipPlugin() {
// 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 && text[start - from - 1] !== '[') start--
while (end < to && text[end - from] !== ']') end++

if (text[start - from - 1] !== '[' || text[end - from] !== ']') {
return null
}

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<a style="text-decoration: none;" href="${DOCUMENTATION_URL_BASE}#${word}" rel="noopener" target="_blank">
<strong>${word}</strong>
</a>documentation`

return { dom: div }
},
}
}
}

return null
})
}
Loading