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
58 changes: 58 additions & 0 deletions src/components/widgets/filesystem/FileEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<script lang="ts">
import { Vue, Component, Prop, Ref } from 'vue-property-decorator'
import type * as Monaco from 'monaco-editor/esm/vs/editor/editor.api'
import getReferenceSection from '@/util/get-reference-section'
let monaco: typeof Monaco // dynamically imported

@Component({})
Expand Down Expand Up @@ -69,6 +70,63 @@ export default class FileEditor extends Vue {
'./setupMonaco'
)
monaco = await promise

monaco.editor.registerCommand('fluidd_open_docs', (_, isMoonrakerConfig, hash) => {
if (isMoonrakerConfig) {
const url = this.$t('app.file_system.url.moonraker_config', { hash }).toString()
window.open(url)
} else {
const url = this.$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 title = isMoonrakerConfig
? this.$t('app.file_system.label.open_moonraker_config').toString()
: this.$t('app.file_system.label.open_klipper_config').toString()

const linesContent = model.getLinesContent()

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

const referenceSection = getReferenceSection(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,
arguments: [isMoonrakerConfig, section.referenceSection]
}
})
),
dispose: () => undefined
}
},
resolveCodeLens: (_model, codeLens) => codeLens
})
}

// Set the correct theme.
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
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
open_klipper_config: Open Klipper config
open_moonraker_config: Open Moonraker config
pedrolamas marked this conversation as resolved.
Show resolved Hide resolved
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
15 changes: 15 additions & 0 deletions src/util/get-reference-section.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const extruderRegExp = /^extruder[0-9]+$/

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

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

return sectionName
}

export default getReferenceSection
1 change: 0 additions & 1 deletion vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ module.exports = defineConfig({
languages: ['markdown'],
features: [
'!codeAction',
'!codelens',
'!colorPicker',
'!contextmenu',
'!folding',
Expand Down