Skip to content

Commit

Permalink
🐛 Fix completions for dimension types
Browse files Browse the repository at this point in the history
Fix #475.
  • Loading branch information
SPGoding committed May 22, 2020
1 parent 5b25f7e commit 75963a3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 36 deletions.
3 changes: 2 additions & 1 deletion src/data/CommandTree1.15.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2316,7 +2316,8 @@ export const CommandTree: ICommandTree = {
parser: new LiteralArgumentParser('in'),
children: {
dimension: {
parser: new IdentityArgumentParser('minecraft:dimension_type'),
// parser: new IdentityArgumentParser('minecraft:dimension_type'),
parser: new IdentityArgumentParser(['minecraft:overworld', 'minecraft:the_end', 'minecraft:the_nether'], undefined, undefined, true),
children: {
subcommand: {
redirect: 'execute_subcommand'
Expand Down
3 changes: 2 additions & 1 deletion src/data/CommandTree1.16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,8 @@ export const CommandTree: ICommandTree = {
parser: new LiteralArgumentParser('in'),
children: {
dimension: {
parser: new IdentityArgumentParser('minecraft:dimension_type'),
// parser: new IdentityArgumentParser('minecraft:dimension_type'),
parser: new IdentityArgumentParser(['minecraft:overworld', 'minecraft:the_end', 'minecraft:the_nether'], undefined, undefined, true),
children: {
subcommand: {
redirect: 'execute_subcommand'
Expand Down
6 changes: 5 additions & 1 deletion src/parsers/IdentityArgumentParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export class IdentityArgumentParser extends ArgumentParser<IdentityNode> {
}
} else {
const registry = registries[this.type]
idPool.push(...Object.keys(registry.entries))
if (registry) {
idPool.push(...Object.keys(registry.entries))
} else {
console.error(`Identity registry ‘${this.type}’ doesn't exist!`)
}
}

const complNamespaces = new Set<string>()
Expand Down
22 changes: 9 additions & 13 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,15 @@ connection.onInitialized(() => {
})

connection.onCompletion(async ({ textDocument: { uri: uriString }, position: { character: char, line: lineNumber } }) => {
try {
const uri = getUri(uriString, uris)
const config = await fetchConfig(uri)
const commandTree = await getCommandTree(config.env.cmdVersion)
const vanillaData = await getVanillaData(config.env.dataVersion, config.env.dataSource, versionInformation, globalStoragePath)
const info = await getInfo(uri, roots, infos, cacheFile, config, fs.readFile, commandTree, vanillaData)
if (info && info.config.features.completions) {
const commandTree = await getCommandTree(info.config.env.cmdVersion)
const vanillaData = await getVanillaData(info.config.env.dataVersion, info.config.env.dataSource, versionInformation, globalStoragePath)
return onCompletion({ cacheFile, lineNumber, char, info, commandTree, vanillaData })
}
} catch (e) {
connection.console.error(e)
const uri = getUri(uriString, uris)
const config = await fetchConfig(uri)
const commandTree = await getCommandTree(config.env.cmdVersion)
const vanillaData = await getVanillaData(config.env.dataVersion, config.env.dataSource, versionInformation, globalStoragePath)
const info = await getInfo(uri, roots, infos, cacheFile, config, fs.readFile, commandTree, vanillaData)
if (info && info.config.features.completions) {
const commandTree = await getCommandTree(info.config.env.cmdVersion)
const vanillaData = await getVanillaData(info.config.env.dataVersion, info.config.env.dataSource, versionInformation, globalStoragePath)
return onCompletion({ cacheFile, lineNumber, char, info, commandTree, vanillaData })
}
return null
})
Expand Down
45 changes: 25 additions & 20 deletions src/utils/handlers/onCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,31 @@ import { constructContext } from '../../types/ParsingContext'
import { StringReader } from '../StringReader'

export async function onCompletion({ char, lineNumber, info, cacheFile, commandTree, vanillaData }: { char: number, lineNumber: number, info: FunctionInfo, cacheFile: CacheFile, commandTree?: CommandTree, vanillaData?: VanillaData }) {
const parser = new LineParser(false, 'line')
const reader = new StringReader(info.strings[lineNumber])
let { data: { completions } } = parser.parse(reader, constructContext({
cursor: char,
cache: cacheFile.cache,
config: info.config,
lineNumber
}, commandTree, vanillaData))
try {
const parser = new LineParser(false, 'line')
const reader = new StringReader(info.strings[lineNumber])
let { data: { completions } } = parser.parse(reader, constructContext({
cursor: char,
cache: cacheFile.cache,
config: info.config,
lineNumber
}, commandTree, vanillaData))

// Escape for TextMate: #431
/* istanbul ignore else */
if (completions) {
completions = completions.map(comp => {
/* istanbul ignore next */
if (comp.insertTextFormat === InsertTextFormat.Snippet) {
return handleCompletionText(comp, str => escapeString(str, null))
}
return comp
})
}
// Escape for TextMate: #431
/* istanbul ignore else */
if (completions) {
completions = completions.map(comp => {
/* istanbul ignore next */
if (comp.insertTextFormat === InsertTextFormat.Snippet) {
return handleCompletionText(comp, str => escapeString(str, null))
}
return comp
})
}

return completions
return completions
} catch (e) {
console.log(e)
}
return null
}

0 comments on commit 75963a3

Please sign in to comment.