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: command signatures #642

Draft
wants to merge 1 commit into
base: dev
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
9 changes: 9 additions & 0 deletions src/components/Languages/Language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface IAddLanguageOptions {
tokenProvider: any
completionItemProvider?: languages.CompletionItemProvider
codeActionProvider?: languages.CodeActionProvider
signatureHelpProvider?: languages.SignatureHelpProvider
}

export abstract class Language {
Expand All @@ -25,6 +26,7 @@ export abstract class Language {
tokenProvider,
completionItemProvider,
codeActionProvider,
signatureHelpProvider,
}: IAddLanguageOptions) {
this.id = id

Expand Down Expand Up @@ -55,6 +57,13 @@ export abstract class Language {
this.disposables.push(
languages.registerCodeActionProvider(id, codeActionProvider)
)
if (signatureHelpProvider)
this.disposables.push(
languages.registerSignatureHelpProvider(
id,
signatureHelpProvider
)
)
})
}

Expand Down
119 changes: 119 additions & 0 deletions src/components/Languages/Mcfunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,124 @@ const completionItemProvider: languages.CompletionItemProvider = {
},
}

const signatureHelpProvider: languages.SignatureHelpProvider = {
signatureHelpTriggerCharacters: ['\n', ' '],
signatureHelpRetriggerCharacters: ['\n', ' '],
provideSignatureHelp: async (
model: editor.ITextModel,
position: Position,
token: CancellationToken,
context: languages.SignatureHelpContext
) => {
// Get the commandData instance so we can access command schema data
const app = await App.getApp()
if (!(app.project instanceof BedrockProject)) return

await app.project.commandData.fired

const commandData = app.project.commandData

// Generate available signatures for the command on this line and figure out where the cursor is inside of the command
let signatures: languages.SignatureInformation[] = []
let signatureIndex = 0
let parameterIndex = 0

let lineContent = ''
try {
lineContent = model.getLineContent(position.lineNumber)
} catch {}

const { tokens } = tokenizeCommand(lineContent)

const commandName = tokens[0].word
if (commandName) {
const definitions = await commandData.getCommandDefinitions(
commandName,
false
)

for (const [defIndex, def] of definitions.entries()) {
const signatureParts: {
signature: string
documentation?: string
}[] = [{ signature: `/${commandName}` }]

for (const arg of def.arguments ?? []) {
// If there is an argument name, create the signature using this name and the type (assumed string if not specified)
// TODO - better display coordinate signatures, currently they can display as 3 args but they count as one
if (arg.argumentName) {
signatureParts.push({
signature: arg.isOptional
? `[${arg.argumentName}: ${
arg.type ?? 'string'
}]`
: `<${arg.argumentName}: ${
arg.type ?? 'string'
}>`,
documentation: arg.description,
})
} else if (
// Otherwise, if there is no argument name, but there is a single enum value for this argument just show the only valid value in the signature
arg.additionalData?.values &&
arg.additionalData?.values.length === 1
) {
signatureParts.push({
signature: arg.additionalData.values[0],
})
}
}
signatures.push({
label: signatureParts
.map((part) => part.signature)
.join(' '),
documentation: def.description,
parameters: signatureParts.map((part) => ({
label: part.signature,
documentation: part.documentation,
})),
})

// Figure out which argument the cursor is placed on
for (const [tokenIndex, token] of tokens.entries()) {
console.log(
token,
tokenIndex,
tokenIndex + 1 === tokens.length &&
position.column >= token.startColumn
)
if (
tokenIndex + 1 === tokens.length &&
position.column >= token.startColumn
) {
parameterIndex = tokenIndex
break
}
if (
position.column >= token.startColumn &&
position.column <= token.endColumn
) {
parameterIndex = tokenIndex
break
}
}

// TODO - Find out which signature to use
// We should probably update this to borrow logic from the function validator
// The validator should be updated so that parsing a command also returns which schema definition(s) passed validation
}
}

return {
dispose: () => {},
value: {
signatures,
activeParameter: parameterIndex,
activeSignature: signatureIndex,
},
}
},
}

const loadCommands = async (lang: McfunctionLanguage) => {
const app = await App.getApp()
await app.projectManager.fired
Expand Down Expand Up @@ -179,6 +297,7 @@ export class McfunctionLanguage extends Language {
config,
tokenProvider,
completionItemProvider,
signatureHelpProvider,
})

let loadedProject: Project | null = null
Expand Down
8 changes: 2 additions & 6 deletions src/components/Languages/Mcfunction/Validator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
tokenizeCommand,
tokenizeTargetSelector,
castType,
} from 'bridge-common-utils'
import { CommandData, ICommandArgument } from './Data'
import { tokenizeCommand } from 'bridge-common-utils'
import { CommandData } from './Data'
import type { editor } from 'monaco-editor'
import { useMonaco } from '/@/utils/libs/useMonaco'
import { RefSchema } from '/@/components/JSONSchema/Schema/Ref'
Expand Down
35 changes: 28 additions & 7 deletions src/components/Languages/Mcfunction/WithinJson.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { App } from '/@/App'
import { getLocation } from '/@/utils/monaco/getLocation'
import type { editor, Position, Range } from 'monaco-editor'
import type {
CancellationToken,
editor,
languages,
Position,
Range,
} from 'monaco-editor'
import { getJsonWordAtPosition } from '/@/utils/monaco/getJsonWord'
import { tokenizeCommand } from 'bridge-common-utils'
import { BedrockProject } from '/@/components/Projects/Project/BedrockProject'
Expand All @@ -25,12 +31,10 @@ export async function registerEmbeddedMcfunctionProvider() {
const currentTab = app.project.tabSystem?.selectedTab
if (!currentTab) return

const validCommands: Record<
string,
string[]
> = await app.dataLoader.readJSON(
`data/packages/minecraftBedrock/location/validCommand.json`
)
const validCommands: Record<string, string[]> =
await app.dataLoader.readJSON(
`data/packages/minecraftBedrock/location/validCommand.json`
)
const {
id,
meta: { commandsUseSlash } = { commandsUseSlash: false },
Expand Down Expand Up @@ -90,4 +94,21 @@ export async function registerEmbeddedMcfunctionProvider() {
}
},
})

// TODO
// languages.registerSignatureHelpProvider('json', {
// signatureHelpTriggerCharacters: ['\n', ' '],
// signatureHelpRetriggerCharacters: ['\n', ' '],
// provideSignatureHelp: async (
// model: editor.ITextModel,
// position: Position,
// token: CancellationToken,
// context: languages.SignatureHelpContext
// ) => {
// return {
// dispose: () => {},
// value: {},
// }
// },
// })
}