Skip to content

Commit

Permalink
LS integration for Rename & Formatting (#1162)
Browse files Browse the repository at this point in the history
  • Loading branch information
nighca authored Dec 24, 2024
1 parent b7639b1 commit ac88d02
Show file tree
Hide file tree
Showing 23 changed files with 461 additions and 180 deletions.
26 changes: 13 additions & 13 deletions spx-gui/src/components/asset/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 })
Expand Down Expand Up @@ -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)
})
},
Expand All @@ -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)
})
},
Expand All @@ -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)
})
},
Expand All @@ -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)
})
},
Expand All @@ -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)
})
},
Expand All @@ -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)
})
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<script lang="ts">
export interface IRenameTarget {
/** Current name */
name: string
/** Validate the new name */
validateName(newName: string): LocaleMessage | null | undefined
setName(newName: string): Promise<void>
/** Apply the new name */
applyName(newName: string): Promise<void>
/** Tip for new name input */
inputTip: LocaleMessage
}
</script>
Expand Down Expand Up @@ -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')
},
Expand Down
10 changes: 6 additions & 4 deletions spx-gui/src/components/editor/code-editor/FormatButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
<script setup lang="ts">
import { UIButton } from '@/components/ui'
import { useMessageHandle } from '@/utils/exception'
import { useEditorCtx } from '../EditorContextProvider.vue'
import { useCodeEditorCtx } from './context'
import { getTextDocumentId } from './common'
const props = defineProps<{
codeFilePath: string
}>()
const editorCtx = useEditorCtx()
const codeEditorCtx = useCodeEditorCtx()
const handleFormat = useMessageHandle(
() => {
const textDocumentId = getTextDocumentId(props.codeFilePath)
return codeEditorCtx.formatTextDocument(textDocumentId)
},
() =>
editorCtx.project.history.doAction({ name: { en: 'Format code', zh: '格式化代码' } }, () =>
codeEditorCtx.formatTextDocument(getTextDocumentId(props.codeFilePath))
),
{
en: 'Failed to format',
zh: '格式化失败'
Expand Down
Loading

0 comments on commit ac88d02

Please sign in to comment.