forked from goplus/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DocumentBase & draft of APIReference (goplus#1094)
- Loading branch information
Showing
15 changed files
with
297 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
spx-gui/src/components/editor/code-editor/document-base/gop.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { | ||
DefinitionKind, | ||
type DefinitionDocumentationItem, | ||
makeBasicMarkdownString, | ||
categoryControlFlow | ||
} from '../common' | ||
|
||
export const forIterate: DefinitionDocumentationItem = { | ||
categories: [categoryControlFlow], | ||
kind: DefinitionKind.Statement, | ||
definition: { name: 'for_iterate (TODO)' }, | ||
insertText: 'for ${1:i}, ${2:v} <- ${3:set} {\n\t${4:}\n}', | ||
overview: 'for i, v <- set {} (TODO)', | ||
detail: makeBasicMarkdownString({ en: 'Iterate within given set', zh: '遍历指定集合' }) | ||
} |
23 changes: 23 additions & 0 deletions
23
spx-gui/src/components/editor/code-editor/document-base/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { type DefinitionIdentifier, type DefinitionDocumentationItem, stringifyDefinitionId } from '../common' | ||
import * as gopDefinitions from './gop' | ||
import * as spxDefinitions from './spx' | ||
|
||
export class DocumentBase { | ||
private storage = new Map<string, DefinitionDocumentationItem>() | ||
|
||
constructor() { | ||
;[...Object.values(gopDefinitions), ...Object.values(spxDefinitions)].forEach((d) => { | ||
this.addDocumentation(d) | ||
}) | ||
} | ||
|
||
private addDocumentation(documentation: DefinitionDocumentationItem) { | ||
const key = stringifyDefinitionId(documentation.definition) | ||
this.storage.set(key, documentation) | ||
} | ||
|
||
async getDocumentation(defId: DefinitionIdentifier): Promise<DefinitionDocumentationItem | null> { | ||
const key = stringifyDefinitionId(defId) | ||
return this.storage.get(key) ?? null | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
spx-gui/src/components/editor/code-editor/document-base/spx.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
DefinitionKind, | ||
type DefinitionDocumentationItem, | ||
makeBasicMarkdownString, | ||
categoryEventGame, | ||
categoryMotionPosition | ||
} from '../common' | ||
|
||
const packageSpx = 'github.com/goplus/spx' | ||
|
||
export const onStart: DefinitionDocumentationItem = { | ||
categories: [categoryEventGame], | ||
kind: DefinitionKind.Listen, | ||
definition: { | ||
package: packageSpx, | ||
name: 'onStart' | ||
}, | ||
insertText: 'onStart => {\n\t${1}\n}', | ||
overview: 'func onStart(callback func())', | ||
detail: makeBasicMarkdownString({ en: 'Listen to game start', zh: '游戏开始时执行' }) | ||
} | ||
|
||
export const setXYpos: DefinitionDocumentationItem = { | ||
categories: [categoryMotionPosition], | ||
kind: DefinitionKind.Command, | ||
definition: { | ||
package: packageSpx, | ||
name: 'Sprite.setXYpos' | ||
}, | ||
insertText: 'setXYpos ${1:X}, ${2:Y}', | ||
overview: 'func setXYpos(x, y float64)', | ||
detail: makeBasicMarkdownString({ en: 'Move to given position', zh: '移动到指定位置' }) | ||
} |
33 changes: 33 additions & 0 deletions
33
spx-gui/src/components/editor/code-editor/ui/APIReference.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<script setup lang="ts"> | ||
import type { APIReference } from '.' | ||
import MarkdownView from './MarkdownView.vue' | ||
defineProps<{ | ||
apiReference: APIReference | ||
}>() | ||
function handleInsert() { | ||
console.warn('TODO: insert') | ||
} | ||
function handleExplain() { | ||
console.warn('TODO: explain') | ||
} | ||
</script> | ||
|
||
<template> | ||
<ul class="api-reference"> | ||
<li v-for="(item, i) in apiReference.items" :key="i" class="item"> | ||
<h4>{{ item.overview }}</h4> | ||
<MarkdownView :value="item.detail.value" /> | ||
<button type="button" @click="handleInsert">insert</button> | ||
<button type="button" @click="handleExplain">explain</button> | ||
</li> | ||
</ul> | ||
</template> | ||
|
||
<style lang="scss" scoped> | ||
.api-reference { | ||
padding: 1em; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
spx-gui/src/components/editor/code-editor/ui/MarkdownView.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<script setup lang="ts"> | ||
import { computed } from 'vue' | ||
import { useI18n, type LocaleMessage } from '@/utils/i18n' | ||
const props = defineProps<{ | ||
value: string | LocaleMessage | ||
}>() | ||
const i18n = useI18n() | ||
const rendered = computed(() => { | ||
const value = props.value | ||
// TODO: | ||
// * markdown renderer | ||
// * basic / advanced support | ||
if (typeof value === 'string') { | ||
return value | ||
} | ||
return i18n.t(value) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div class="markdown-view"> | ||
{{ rendered }} | ||
</div> | ||
</template> | ||
|
||
<style lang="scss" scoped></style> |
29 changes: 27 additions & 2 deletions
29
spx-gui/src/components/editor/code-editor/ui/api-reference.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
import { type BaseContext, type Position, type DocumentationItem } from '../common' | ||
import { shallowReactive } from 'vue' | ||
import { Disposable } from '@/utils/disposable' | ||
import { type BaseContext, type Position, type DefinitionDocumentationItem } from '../common' | ||
import type { CodeEditorUI } from '.' | ||
|
||
export type APIReferenceItem = DocumentationItem | ||
export type APIReferenceItem = DefinitionDocumentationItem | ||
|
||
export type APIReferenceContext = BaseContext | ||
|
||
export interface IAPIReferenceProvider { | ||
provideAPIReference(ctx: APIReferenceContext, position: Position): Promise<APIReferenceItem[]> | ||
} | ||
|
||
export class APIReference extends Disposable { | ||
items: APIReferenceItem[] = shallowReactive([]) | ||
|
||
private provider: IAPIReferenceProvider | null = null | ||
registerProvider(provider: IAPIReferenceProvider) { | ||
this.provider = provider | ||
} | ||
|
||
constructor(private ui: CodeEditorUI) { | ||
super() | ||
} | ||
|
||
async init(signal: AbortSignal) { | ||
const context: APIReferenceContext = { | ||
textDocument: this.ui.activeTextDocument!, | ||
signal | ||
} | ||
const items = await this.provider!.provideAPIReference(context, { line: 0, column: 0 }) | ||
this.items.splice(0, this.items.length, ...items) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.