From 2d261e9ba240f58658ca95202947b9b3a7bd72af Mon Sep 17 00:00:00 2001 From: nighca Date: Fri, 20 Dec 2024 15:42:21 +0800 Subject: [PATCH] LS integration for Rename & Formatting --- spx-gui/src/components/asset/index.ts | 26 +- .../{asset => common}/RenameModal.vue | 8 +- .../editor/code-editor/FormatButton.vue | 10 +- .../editor/code-editor/code-editor.ts | 273 ++++++++++++------ .../components/editor/code-editor/common.ts | 17 ++ .../components/editor/code-editor/context.ts | 20 +- .../editor/code-editor/document-base/index.ts | 4 + .../editor/code-editor/lsp/index.ts | 31 +- .../editor/code-editor/ui/CodeEditorUI.vue | 34 ++- .../ui/api-reference/APIReferenceUI.vue | 1 + .../code-editor/ui/api-reference/index.ts | 5 + .../editor/code-editor/ui/code-editor-ui.ts | 30 +- .../editor/code-editor/ui/common.ts | 16 - .../code-editor/ui/context-menu/index.ts | 4 +- .../editor/code-editor/ui/formatting.ts | 8 - .../editor/code-editor/ui/hover/index.ts | 5 +- .../editor/preview/EditorPreview.vue | 42 ++- spx-gui/src/models/common/asset-name.ts | 12 +- spx-gui/src/utils/spx/highlighter.ts | 3 + spx-gui/src/utils/spx/index.ts | 20 ++ tools/spxls/internal/server/command.go | 4 + tools/spxls/internal/server/rename.go | 17 +- tools/spxls/internal/server/rename_test.go | 51 ++++ 23 files changed, 461 insertions(+), 180 deletions(-) rename spx-gui/src/components/{asset => common}/RenameModal.vue (90%) delete mode 100644 spx-gui/src/components/editor/code-editor/ui/formatting.ts diff --git a/spx-gui/src/components/asset/index.ts b/spx-gui/src/components/asset/index.ts index 649f540ff..a5659dea1 100644 --- a/spx-gui/src/components/asset/index.ts +++ b/spx-gui/src/components/asset/index.ts @@ -17,6 +17,7 @@ import { Monitor } from '@/models/widget/monitor' import * as assetName from '@/models/common/asset-name' import type { Costume } from '@/models/costume' import type { Widget } from '@/models/widget' +import RenameModal from '../common/RenameModal.vue' import SoundRecorderModal from '../editor/sound/SoundRecorderModal.vue' import { useEditorCtx } from '../editor/EditorContextProvider.vue' import { useCodeEditorCtx } from '../editor/code-editor/context' @@ -26,7 +27,6 @@ import AssetAddModal from './library/AssetAddModal.vue' import LoadFromScratchModal from './scratch/LoadFromScratchModal.vue' import PreprocessModal from './preprocessing/PreprocessModal.vue' import GroupCostumesModal from './animation/GroupCostumesModal.vue' -import RenameModal from './RenameModal.vue' function selectAsset(project: Project, asset: AssetModel | undefined) { if (asset instanceof Sprite) project.select({ type: 'sprite', id: asset.id }) @@ -206,10 +206,10 @@ export function useRenameSprite() { validateName(name) { return assetName.validateSpriteName(name, editorCtx.project) }, - async setName(newName) { + async applyName(newName) { const action = { name: { en: 'Rename sprite', zh: '重命名精灵' } } await editorCtx.project.history.doAction(action, async () => { - await codeEditorCtx.updateResourceReferencesOnRename(getResourceIdentifier(sprite), newName) + await codeEditorCtx.renameResource(getResourceIdentifier(sprite), newName) sprite.setName(newName) }) }, @@ -230,10 +230,10 @@ export function useRenameSound() { validateName(name) { return assetName.validateSoundName(name, editorCtx.project) }, - async setName(newName) { + async applyName(newName) { const action = { name: { en: 'Rename sound', zh: '重命名声音' } } await editorCtx.project.history.doAction(action, async () => { - await codeEditorCtx.updateResourceReferencesOnRename(getResourceIdentifier(sound), newName) + await codeEditorCtx.renameResource(getResourceIdentifier(sound), newName) sound.setName(newName) }) }, @@ -254,10 +254,10 @@ export function useRenameCostume() { validateName(name) { return assetName.validateCostumeName(name, costume.parent) }, - async setName(newName) { + async applyName(newName) { const action = { name: { en: 'Rename costume', zh: '重命名造型' } } await editorCtx.project.history.doAction(action, async () => { - await codeEditorCtx.updateResourceReferencesOnRename(getResourceIdentifier(costume), newName) + await codeEditorCtx.renameResource(getResourceIdentifier(costume), newName) costume.setName(newName) }) }, @@ -278,10 +278,10 @@ export function useRenameBackdrop() { validateName(name) { return assetName.validateBackdropName(name, editorCtx.project.stage) }, - async setName(newName) { + async applyName(newName) { const action = { name: { en: 'Rename backdrop', zh: '重命名背景' } } await editorCtx.project.history.doAction(action, async () => { - await codeEditorCtx.updateResourceReferencesOnRename(getResourceIdentifier(backdrop), newName) + await codeEditorCtx.renameResource(getResourceIdentifier(backdrop), newName) backdrop.setName(newName) }) }, @@ -302,10 +302,10 @@ export function useRenameAnimation() { validateName(name) { return assetName.validateAnimationName(name, animation.sprite) }, - async setName(newName) { + async applyName(newName) { const action = { name: { en: 'Rename animation', zh: '重命名动画' } } await editorCtx.project.history.doAction(action, async () => { - await codeEditorCtx.updateResourceReferencesOnRename(getResourceIdentifier(animation), newName) + await codeEditorCtx.renameResource(getResourceIdentifier(animation), newName) animation.setName(newName) }) }, @@ -326,10 +326,10 @@ export function useRenameWidget() { validateName(name) { return assetName.validateWidgetName(name, editorCtx.project.stage) }, - async setName(newName) { + async applyName(newName) { const action = { name: { en: 'Rename widget', zh: '重命名控件' } } await editorCtx.project.history.doAction(action, async () => { - await codeEditorCtx.updateResourceReferencesOnRename(getResourceIdentifier(widget), newName) + await codeEditorCtx.renameResource(getResourceIdentifier(widget), newName) widget.setName(newName) }) }, diff --git a/spx-gui/src/components/asset/RenameModal.vue b/spx-gui/src/components/common/RenameModal.vue similarity index 90% rename from spx-gui/src/components/asset/RenameModal.vue rename to spx-gui/src/components/common/RenameModal.vue index 312b3b4c8..ce65afdc2 100644 --- a/spx-gui/src/components/asset/RenameModal.vue +++ b/spx-gui/src/components/common/RenameModal.vue @@ -1,8 +1,12 @@ @@ -31,7 +35,7 @@ const form = useForm({ const handleSubmit = useMessageHandle( async () => { if (form.value.name !== props.target.name) { - await props.target.setName(form.value.name) + await props.target.applyName(form.value.name) } emit('resolved') }, diff --git a/spx-gui/src/components/editor/code-editor/FormatButton.vue b/spx-gui/src/components/editor/code-editor/FormatButton.vue index 89d9cd21b..1ac8c2521 100644 --- a/spx-gui/src/components/editor/code-editor/FormatButton.vue +++ b/spx-gui/src/components/editor/code-editor/FormatButton.vue @@ -7,6 +7,7 @@