Skip to content

Commit

Permalink
🐛 Fix obsolete data when cache is updated
Browse files Browse the repository at this point in the history
Fix #421. Fix #653.
  • Loading branch information
SPGoding committed Aug 15, 2020
1 parent 38b4267 commit 983ee2e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ This project follows the [all-contributors](https://github.com/all-contributors/
</tr>
<tr>
<td align="center"><a href="https://github.com/misode"><img src="https://avatars1.githubusercontent.com/u/17352009?s=460&u=2813225036a78ea0c585fa5f9150d448c3a8ff8e&v=4" width="64px;" alt=""/><br /><sub><b>Misode</b></sub></a></td><td align="left"><a href="https://github.com/misode/minecraft-schemas" target="_blank" title="Dependency">⬆️ Maintains minecraft-schemas repository</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/445" target="_blank" title="Ideas, Planning, and Feedback">✨ #445</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/592" target="_blank" title="Bug reports">🐛 #592</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/593" target="_blank" title="Bug reports">🐛 #593</a></td>
<td align="center"><a href="https://github.com/NeunEinser"><img src="https://avatars3.githubusercontent.com/u/12124394?s=460&v=4" width="64px;" alt=""/><br /><sub><b>NeunEinser</b></sub></a></td><td align="left"><a href="https://github.com/SPGoding/datapack-language-server/commits?author=NeunEinser" target="_blank" title="Localization">🌐 German (de)</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/652" target="_blank" title="Bug reports">🐛 #652</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/654" target="_blank" title="Bug reports">🐛 #654</a></td>
<td align="center"><a href="https://github.com/NeunEinser"><img src="https://avatars3.githubusercontent.com/u/12124394?s=460&v=4" width="64px;" alt=""/><br /><sub><b>NeunEinser</b></sub></a></td><td align="left"><a href="https://github.com/SPGoding/datapack-language-server/commits?author=NeunEinser" target="_blank" title="Localization">🌐 German (de)</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/652" target="_blank" title="Bug reports">🐛 #652</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/653" target="_blank" title="Bug reports">🐛 #653</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/654" target="_blank" title="Bug reports">🐛 #654</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/Yurihaia"><img src="https://avatars3.githubusercontent.com/u/17830663?s=400&u=4959d74e027642f5a207dcd5e112005c5932b844&v=4" width="64px;" alt=""/><br /><sub><b>Yurihaia</b></sub></a></td><td align="left"><a href="https://github.com/Yurihaia/mc-nbtdoc" target="_blank" title="Dependency">⬆️ Maintains mc-nbtdoc repository</a></td>
Expand Down
2 changes: 2 additions & 0 deletions contributors/.all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
{ "type": "localization", "link": "commits?author=ChenCMD", "text": "Japanese (ja)" },
{ "type": "ideas", "link": "issues/410", "text": "#410" },
{ "type": "ideas", "link": "issues/582", "text": "#582" },
{ "type": "bug", "link": "issues/421", "text": "#421" },
{ "type": "bug", "link": "issues/534", "text": "#534" },
{ "type": "bug", "link": "issues/589", "text": "#589" },
{ "type": "bug", "link": "issues/590", "text": "#590" },
Expand Down Expand Up @@ -82,6 +83,7 @@
"details": [
{ "type": "localization", "link": "commits?author=NeunEinser", "text": "German (de)" },
{ "type": "bug", "link": "issues/652", "text": "#652" },
{ "type": "bug", "link": "issues/653", "text": "#653" },
{ "type": "bug", "link": "issues/654", "text": "#654" }
]
},
Expand Down
2 changes: 1 addition & 1 deletion contributors/3.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A big _thank you_ goes to these wonderful people! (sorted by the Unicode of name
</tr>
<tr>
<td align="center"><a href="https://github.com/misode"><img src="https://avatars1.githubusercontent.com/u/17352009?s=460&u=2813225036a78ea0c585fa5f9150d448c3a8ff8e&v=4" width="64px;" alt=""/><br /><sub><b>Misode</b></sub></a></td><td align="left"><a href="https://github.com/misode/minecraft-schemas" target="_blank" title="Dependency">⬆️ Maintains minecraft-schemas repository</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/445" target="_blank" title="Ideas, Planning, and Feedback">✨ #445</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/592" target="_blank" title="Bug reports">🐛 #592</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/593" target="_blank" title="Bug reports">🐛 #593</a></td>
<td align="center"><a href="https://github.com/NeunEinser"><img src="https://avatars3.githubusercontent.com/u/12124394?s=460&v=4" width="64px;" alt=""/><br /><sub><b>NeunEinser</b></sub></a></td><td align="left"><a href="https://github.com/SPGoding/datapack-language-server/commits?author=NeunEinser" target="_blank" title="Localization">🌐 German (de)</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/652" target="_blank" title="Bug reports">🐛 #652</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/654" target="_blank" title="Bug reports">🐛 #654</a></td>
<td align="center"><a href="https://github.com/NeunEinser"><img src="https://avatars3.githubusercontent.com/u/12124394?s=460&v=4" width="64px;" alt=""/><br /><sub><b>NeunEinser</b></sub></a></td><td align="left"><a href="https://github.com/SPGoding/datapack-language-server/commits?author=NeunEinser" target="_blank" title="Localization">🌐 German (de)</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/652" target="_blank" title="Bug reports">🐛 #652</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/653" target="_blank" title="Bug reports">🐛 #653</a><br><a href="https://github.com/SPGoding/datapack-language-server/issues/654" target="_blank" title="Bug reports">🐛 #654</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/Yurihaia"><img src="https://avatars3.githubusercontent.com/u/17830663?s=400&u=4959d74e027642f5a207dcd5e112005c5932b844&v=4" width="64px;" alt=""/><br /><sub><b>Yurihaia</b></sub></a></td><td align="left"><a href="https://github.com/Yurihaia/mc-nbtdoc" target="_blank" title="Dependency">⬆️ Maintains mc-nbtdoc repository</a></td>
Expand Down
2 changes: 2 additions & 0 deletions src/nodes/IdentityNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ export class IdentityNode extends ArgumentNode {
let ext: string
if (category === 'function') {
ext = '.mcfunction'
} else if (category === 'structure') {
ext = '.nbt'
} else {
ext = '.json'
}
Expand Down
38 changes: 28 additions & 10 deletions src/services/DatapackLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class DatapackLanguageService {

private readonly builders: Map<string, lsp.ProposedFeatures.SemanticTokensBuilder> = new Map()
/**
* Key: `${uriString}|${range}`
* Key: `${type}|${ID}`
*/
private readonly caches: Map<string, ClientCache> = new Map()
private readonly configs: Map<string, Config> = new Map()
Expand All @@ -72,6 +72,10 @@ export class DatapackLanguageService {
private languageConfigs: Map<string, LanguageConfig>
private contributions: Contributions

private static readonly ConfigCacheSize = 100
private static readonly OnDidUpdateCacheEventDelay = 1500
private onDidUpdateCacheTimeout: NodeJS.Timeout | undefined

static readonly GeneralTriggerCharacters = [' ', '=', ':', '/', '!', "'", '"', '.', '@']
static readonly McfunctionTriggerCharacters = [',', '{', '[']
static readonly AllTriggerCharacters = DatapackLanguageService.GeneralTriggerCharacters.concat(DatapackLanguageService.McfunctionTriggerCharacters)
Expand Down Expand Up @@ -111,7 +115,7 @@ export class DatapackLanguageService {
async fetchConfig(uri: Uri) {
const config = await this.rawFetchConfig(uri)
this.configs.set(uri.toString(), config)
if (this.configs.size > 100) {
if (this.configs.size > DatapackLanguageService.ConfigCacheSize) {
this.configs.clear()
}
return config
Expand Down Expand Up @@ -301,7 +305,8 @@ export class DatapackLanguageService {
return
}
this.onDidCloseTextDocument(uri)
this.parseDocument(textDoc, true)
await this.parseDocument(textDoc, true)
return this.publishDiagnostics(uri)
}

/**
Expand Down Expand Up @@ -336,9 +341,9 @@ export class DatapackLanguageService {
return { doc: await this.parseDocument(textDoc, false), textDoc }
}
} catch (e) {
console.error('[getDocuments]', e)
return { doc: undefined, textDoc: undefined }
console.error(`[getDocuments] for ${uri} `, e)
}
return { doc: undefined, textDoc: undefined }
}

private getLangID(uri: Uri): 'json' | 'mcfunction' | 'nbt' {
Expand Down Expand Up @@ -665,7 +670,9 @@ export class DatapackLanguageService {
if (!node) {
return null
}
return onRenameRequest({ node, offset, newName, service: this })
const ans = await onRenameRequest({ node, offset, newName, service: this })
this.onDidUpdateCache()
return ans
}

async onDocumentLinks(uri: Uri) {
Expand Down Expand Up @@ -772,18 +779,25 @@ export class DatapackLanguageService {

private onDidUpdateCache() {
this.caches.clear()
if (this.onDidUpdateCacheTimeout) {
clearTimeout(this.onDidUpdateCacheTimeout)
}
this.onDidUpdateCacheTimeout = setTimeout(
this.reparseAllOpenDocuments.bind(this),
DatapackLanguageService.OnDidUpdateCacheEventDelay
)
}

private onDidIDUpdate(type: FileType, id: IdentityNode) {
this.urisOfIds.delete(`${type}|${id}`)
}

private removeCachePositionsWith(uri: Uri) {
this.onDidUpdateCache()
removeCachePosition(this.cacheFile.cache, uri)
}

private async combineCacheOfNodes(uri: Uri, type: FileType, id: IdentityNode) {
console.log(`combineCacheOfNodes ${uri}`)
const { doc, textDoc } = await this.getDocuments(uri)
if (doc && textDoc) {
const cacheOfNodes: ClientCache = {}
Expand Down Expand Up @@ -821,6 +835,7 @@ export class DatapackLanguageService {
if (!this.isOpen(uri)) {
return this.mergeFileCacheIntoGlobalCache(uri)
}
return
}

private async mergeFileCacheIntoGlobalCache(uri: Uri) {
Expand All @@ -836,7 +851,8 @@ export class DatapackLanguageService {
return
}
this.removeCachePositionsWith(uri)
return this.combineCacheOfNodes(uri, category, id)
this.onDidUpdateCache()
await this.combineCacheOfNodes(uri, category, id)
}

/**
Expand All @@ -847,6 +863,10 @@ export class DatapackLanguageService {
* @param uri A URI object.
*/
onDeletedFile(uri: Uri) {
this.onDidCloseTextDocument(uri)
this.removeCachePositionsWith(uri)
this.onDidUpdateCache()
delete this.cacheFile.files[uri.toString()]
const rel = this.getRel(uri)
const result = IdentityNode.fromRel(rel)
if (!result) {
Expand All @@ -855,8 +875,6 @@ export class DatapackLanguageService {
const { category, id } = result
this.onDidIDUpdate(category, id)
removeCacheUnit(this.cacheFile.cache, category, id.toString())
this.removeCachePositionsWith(uri)
delete this.cacheFile.files[uri.toString()]
}

private createBuilder(uri: Uri) {
Expand Down
52 changes: 26 additions & 26 deletions src/services/onRenameRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { DocNode, Uri } from '../types/handlers'
import { DatapackLanguageService } from './DatapackLanguageService'

export async function onRenameRequest({ node, offset, newName, service }: { node: DocNode, offset: number, newName: string, service: DatapackLanguageService }): Promise<WorkspaceEdit | null> {
// console.log(`BR: ${JSON.stringify(service.cacheFile)}`)

/* istanbul ignore next */
const result = getCacheFromOffset(node.cache || {}, offset)
if (result && !isInternalType(result.type)) {
Expand All @@ -19,27 +17,31 @@ export async function onRenameRequest({ node, offset, newName, service }: { node
try {
const newID = isNamespacedType(result.type) ? IdentityNode.fromString(newName).toString() : newName

// Change function contents.
// Change file contents.
for (const key of Object.keys(unit)) {
if (isCacheUnitPositionType(key)) {
for (const pos of unit[key] ?? []) {
if (pos.startLine !== pos.endLine || pos.endChar! - pos.startChar! === 0) {
continue
}
const affectedUri = service.parseUri(pos.uri!)
const { textDoc: affectedTextDoc } = await service.getDocuments(affectedUri)
/* istanbul ignore else */
if (affectedTextDoc) {
documentChanges.push({
textDocument: { uri: pos.uri!, version: affectedTextDoc.version },
edits: [{
newText: newName,
range: {
start: { line: pos.startLine!, character: pos.startChar! },
end: { line: pos.startLine!, character: pos.endChar! }
}
}]
})
try {
const { textDoc: affectedTextDoc } = await service.getDocuments(affectedUri)
/* istanbul ignore else */
if (affectedTextDoc) {
documentChanges.push({
textDocument: { uri: pos.uri!, version: affectedTextDoc.version },
edits: [{
newText: newName,
range: {
start: { line: pos.startLine!, character: pos.startChar! },
end: { line: pos.startLine!, character: pos.endChar! }
}
}]
})
}
} catch (e) {
console.error('[onRenameRequest] ', e)
}
}
}
Expand All @@ -51,6 +53,7 @@ export async function onRenameRequest({ node, offset, newName, service }: { node
const oldUri = await service.getUriFromId(oldID, result.type)
/* istanbul ignore next */
if (!oldUri) {
console.error(`[onRenameRequest] Failed to get the URI of “${result.type} ${oldID}”.`)
return null
}

Expand All @@ -64,6 +67,7 @@ export async function onRenameRequest({ node, offset, newName, service }: { node
}
/* istanbul ignore next */
if (!preferredRoot) {
console.error(`[onRenameRequest] Failed to get the preferred root for “${oldRel}” in ${service.roots.map(v => `“${v}”`).join(', ')}.`)
return null
}

Expand All @@ -78,34 +82,30 @@ export async function onRenameRequest({ node, offset, newName, service }: { node
delete service.cacheFile.files[oldUri.toString()]
}

/* istanbul ignore else */
if (result.type === 'function') {
removeCachePosition(service.cacheFile.cache, oldUri)
}
removeCachePosition(service.cacheFile.cache, oldUri)
service.onDidCloseTextDocument(oldUri)
}

// Update cache.
const targetUnit = category[newID]
if (targetUnit) {
for (const t of CacheUnitPositionTypes) {
if (unit[t]?.length) {
targetUnit[t] = targetUnit[t] ?? []
targetUnit[t]!.push(...unit[t]!)
(targetUnit[t] = targetUnit[t] ?? []).push(...unit[t]!)
}
}
} else {
category[newID] = unit
service.cacheFile.cache[result.type] = category
}
delete category[result.id]
} catch (ignored) {
service.cacheFile.cache[result.type] = category
} catch (e) {
/* istanbul ignore next */
console.error('[onRenameRequest] ', e)
return null
}
}

// console.log(`DC: ${JSON.stringify(documentChanges)}`)
// console.log(`AR: ${JSON.stringify(service.cacheFile)}`)
return { documentChanges }
}

Expand Down

0 comments on commit 983ee2e

Please sign in to comment.