Skip to content

Commit

Permalink
πŸ› Fix loading wrong info for non-opened documents (#527)
Browse files Browse the repository at this point in the history
* πŸ› Fix loading wrong info for non-opened documents

Also resolve #519.

* βœ… Update tests

* πŸ‘₯ Update contributors
  • Loading branch information
SPGoding authored Jun 26, 2020
1 parent 8f8dd1e commit 50f5dd5
Showing 9 changed files with 33 additions and 24 deletions.
2 changes: 2 additions & 0 deletions contributors/.all-contributorsrc
Original file line number Diff line number Diff line change
@@ -55,8 +55,10 @@
{ "type": "ideas", "link": "issues/402", "text": "#402" },
{ "type": "bug", "link": "issues/458", "text": "#458" },
{ "type": "bug", "link": "issues/487", "text": "#487" },
{ "type": "bug", "link": "issues/515", "text": "#515" },
{ "type": "bug", "link": "issues/516", "text": "#516" },
{ "type": "bug", "link": "issues/517", "text": "#517" },
{ "type": "bug", "link": "issues/519", "text": "#519" },
{ "type": "bug", "link": "issues/520", "text": "#520" }
]
},
2 changes: 1 addition & 1 deletion contributors/2.1.0.md
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ A big _thank you_ goes to these wonderful people! (sorted by the Unicode of name
<td align="center"><a href="https://www.mcbbs.net/home.php?mod=space&uid=1834567"><img src="https://www.mcbbs.net/uc_server/avatar.php?uid=1834567&size=middle" width="64px;" alt=""/><br /><sub><b>ChapterII</b></sub></a></td><td align="left"><a href="https://github.com/SPGoding/datapack-language-server/issues/467" target="_blank" title="Bug reports">πŸ› #467</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/ChenCMD"><img src="https://avatars2.githubusercontent.com/u/46134240?s=460&u=ca934b86e5189ea9c598a51358571e777e21aa2f&v=4" width="64px;" alt=""/><br /><sub><b>ChenCMD</b></sub></a></td><td align="left"><a href="https://github.com/SPGoding/datapack-language-server/issues/402" target="_blank" title="Ideas, Planning, and Feedback">✨ #402</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/458" target="_blank" title="Bug reports">πŸ› #458</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/487" target="_blank" title="Bug reports">πŸ› #487</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/516" target="_blank" title="Bug reports">πŸ› #516</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/517" target="_blank" title="Bug reports">πŸ› #517</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/520" target="_blank" title="Bug reports">πŸ› #520</a></td>
<td align="center"><a href="https://github.com/ChenCMD"><img src="https://avatars2.githubusercontent.com/u/46134240?s=460&u=ca934b86e5189ea9c598a51358571e777e21aa2f&v=4" width="64px;" alt=""/><br /><sub><b>ChenCMD</b></sub></a></td><td align="left"><a href="https://github.com/SPGoding/datapack-language-server/issues/402" target="_blank" title="Ideas, Planning, and Feedback">✨ #402</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/458" target="_blank" title="Bug reports">πŸ› #458</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/487" target="_blank" title="Bug reports">πŸ› #487</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/515" target="_blank" title="Bug reports">πŸ› #515</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/516" target="_blank" title="Bug reports">πŸ› #516</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/517" target="_blank" title="Bug reports">πŸ› #517</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/519" target="_blank" title="Bug reports">πŸ› #519</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/520" target="_blank" title="Bug reports">πŸ› #520</a></td>
<td align="center"><a href="https://afdian.net/u/da477488928011ea8ea552540025c377"><img src="https://pic1.afdiancdn.com/default/avatar/avatar-purple.png?imageView2/1/w/240/h/240" width="64px;" alt=""/><br /><sub><b>ColorRain_Tree</b></sub></a></td><td align="left"><a href="https://github.com/SPGoding/datapack-language-server/blob/master/CONTRIBUTING.md" target="_blank" title="Financial support">πŸ’› <span style="color:gold;">(οΌƒΒ°Π”Β°)</span></a></td>
</tr>
<tr>
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
@@ -132,6 +132,7 @@
"quote": "a quote (β€˜'’ or β€˜\"’)",
"score-holder": "a score holder",
"server.checking-file": "Checking β€˜%0%’",
"server.fixing-workspace": "Fixing all auto-fixable problems in the workspace",
"server.initializing": "Initialize DHP features",
"server.new-version": "The Datapack Language Server has been updated to a newer version: %0%",
"server.regenerating-cache": "Regenerating cache",
2 changes: 0 additions & 2 deletions src/parsers/EntityArgumentParser.ts
Original file line number Diff line number Diff line change
@@ -416,8 +416,6 @@ export class EntityArgumentParser extends ArgumentParser<EntityNode> {
}

export function getNbtdocRegistryId(entity: EntityNode): null | string {
console.log(require('util').inspect(entity, true, null))

if (entity.variable === 'a' || entity.variable === 'p' || entity.variable === 'r') {
return 'minecraft:player'
}
11 changes: 8 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -576,14 +576,16 @@ connection.onInitialized(() => {
})
break
}
case 'datapack.fixWorkspace':
case 'datapack.fixWorkspace': {
const progress = await connection.window.createWorkDoneProgress()
progress.begin(locale('server.fixing-workspace'))
for (const root of roots) {
const dataPath = path.join(root.fsPath, 'data')
const namespaces = fs.pathExistsSync(dataPath) ? await fs.readdir(dataPath) : []
for (const namespace of namespaces) {
const namespacePath = path.join(dataPath, namespace)
const functionsPath = path.join(namespacePath, 'functions')
walk(root.fsPath, functionsPath, async abs => {
await walk(root.fsPath, functionsPath, async abs => {
try {
const uri = getUri(Uri.file(abs).toString(), uris)
await fixFileCommandHandler({
@@ -599,8 +601,10 @@ connection.onInitialized(() => {
})
}
}
progress.done()
break
case 'datapack.regenerateCache':
}
case 'datapack.regenerateCache': {
const progress = await connection.window.createWorkDoneProgress()
progress.begin(locale('server.regenerating-cache'))

@@ -609,6 +613,7 @@ connection.onInitialized(() => {

progress.done()
break
}
default:
throw new Error(`Unknown β€˜workspace/executeCommand’ request for β€˜${command}’.`)
}
2 changes: 1 addition & 1 deletion src/test/utils/handlers/onDidCloseTextDocument.spec.ts
Original file line number Diff line number Diff line change
@@ -13,6 +13,6 @@ describe('onDidCloseTextDocument() Tests', () => {

onDidCloseTextDocument({ uri, infos })

assert(infos.size === 1)
assert(infos.size === 0)
})
})
5 changes: 0 additions & 5 deletions src/utils/handlers/commands/fixFileCommandHandler.ts
Original file line number Diff line number Diff line change
@@ -10,12 +10,7 @@ export async function fixFileCommandHandler({ uri, roots, infos, cacheFile, read
const info = await getOrCreateInfo(uri, roots, infos, cacheFile, config, readFile, commandTree, vanillaData)
/* istanbul ignore else */
if (info) {
const startTime = new Date().getTime()
const edit = getMergedPreferredEdit(info, uri)
const endTime = new Date().getTime()
console.log(`--------------- Edit for β€˜${uri.toString()}’ (${endTime - startTime} ms) ---------------`)
console.log(JSON.stringify(edit, undefined, 4))

if (edit) {
applyEdit(edit)
}
28 changes: 18 additions & 10 deletions src/utils/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -153,20 +153,28 @@ export function getInfo(uri: Uri, infos: InfosOfUris): FunctionInfo | undefined
return infos.get(uri)
}

/* istanbul ignore next */
export async function createInfo(uri: Uri, roots: Uri[], infos: InfosOfUris, cacheFile: CacheFile, config: Config, readFile: ReadFileFunction, commandTree?: CommandTree, vanillaData?: VanillaData): Promise<FunctionInfo | undefined> {
try {
const rel = getRel(uri, roots)!
if (isRelIncluded(rel, config)) {
const text = await readFile(uri.fsPath, 'utf8')
await onDidOpenTextDocument({ text, uri, rel, infos, config, cacheFile, version: null, roots, commandTree, vanillaData })
const info = infos.get(uri)
infos.delete(uri)
return info
}
} catch (e) {
console.error('createInfo', e)
}
return undefined
}

export async function getOrCreateInfo(uri: Uri, roots: Uri[], infos: InfosOfUris, cacheFile: CacheFile, config: Config, readFile: ReadFileFunction, commandTree?: CommandTree, vanillaData?: VanillaData): Promise<FunctionInfo | undefined> {
let info = infos.get(uri)

if (!info) {
try {
const rel = getRel(uri, roots)!
if (isRelIncluded(rel, config)) {
const text = await readFile(uri.fsPath, 'utf8')
await onDidOpenTextDocument({ text, uri, rel, infos, config, cacheFile, version: null, roots, commandTree, vanillaData })
info = infos.get(uri)
}
} catch (ignored) {
// Ignored.
}
info = await createInfo(uri, roots, infos, cacheFile, config, readFile, commandTree, vanillaData)
}

return info
4 changes: 2 additions & 2 deletions src/utils/handlers/onDidCloseTextDocument.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InfosOfUris, Uri } from '../../types/handlers'

export function onDidCloseTextDocument({ }: { uri: Uri, infos: InfosOfUris }) {
// infos.delete(uri)
export function onDidCloseTextDocument({ infos, uri }: { uri: Uri, infos: InfosOfUris }) {
infos.delete(uri)
}

0 comments on commit 50f5dd5

Please sign in to comment.