Skip to content

Commit

Permalink
🐛 Fix TypeError in getVanillaData
Browse files Browse the repository at this point in the history
Fix #480.
  • Loading branch information
SPGoding committed May 30, 2020
1 parent 57df815 commit b12061d
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 138 deletions.
8 changes: 8 additions & 0 deletions contributors/.all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
{ "type": "coffee", "link": "blob/master/CONTRIBUTING.md", "text": "Donate coffee" }
]
},
{
"name": "UltraBlack_",
"avatar_url": "https://cdn.discordapp.com/avatars/579619944602271744/3cefa458a174a0e15020dd6773c8da3b.webp?size=256",
"contributions": [],
"details": [
{ "type": "bug", "link": "issues/480", "text": "#480" }
]
},
{
"name": "Yurihaia",
"avatar_url": "https://avatars3.githubusercontent.com/u/17830663?s=400&u=4959d74e027642f5a207dcd5e112005c5932b844&v=4",
Expand Down
5 changes: 3 additions & 2 deletions contributors/2.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ A big _thank you_ goes to these wonderful people! (sorted by the Unicode of name
</tr>
<tr>
<td align="center"><a href="https://afdian.net/@k_bai"><img src="https://pic1.afdiancdn.com/user/f34c5d62954411e8948a52540025c377/avatar/a08952a177bcf9aa806e710c0d695dc3_w719_h720_s657.jpg?imageView2/1/w/240/h/240" width="64px;" alt=""/><br /><sub><b>K_bai</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">☕ Donate coffee</a></td>
<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>
<td align="center"><a href=""><img src="https://cdn.discordapp.com/avatars/579619944602271744/3cefa458a174a0e15020dd6773c8da3b.webp?size=256" width="64px;" alt=""/><br /><sub><b>UltraBlack_</b></sub></a></td><td align="left"><a href="https://github.com/SPGoding/datapack-language-server/issues/480" target="_blank" title="Bug reports">🐛 #480</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>
<td align="center"><a href="https://www.mcbbs.net/home.php?mod=space&uid=10240"><img src="https://www.mcbbs.net/uc_server/avatar.php?uid=10240&size=middle" width="64px;" alt=""/><br /><sub><b>kakagRou</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">☕ Donate coffee</a></td>
<td align="center"><a href="https://afdian.net/u/43f152b898b511e8b5f552540025c377"><img src="https://pic1.afdiancdn.com/user/43f152b898b511e8b5f552540025c377/avatar/b35473b4f5d00311d3a3cabef38c60fd_w640_h640_s24.jpg?imageView2/1/w/240/h/240" width="64px;" alt=""/><br /><sub><b>酱香卤鹅</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">☕ Donate coffee</a></td>
</tr>
<tr>
<td align="center"><a href="https://afdian.net/u/43f152b898b511e8b5f552540025c377"><img src="https://pic1.afdiancdn.com/user/43f152b898b511e8b5f552540025c377/avatar/b35473b4f5d00311d3a3cabef38c60fd_w640_h640_s24.jpg?imageView2/1/w/240/h/240" width="64px;" alt=""/><br /><sub><b>酱香卤鹅</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">☕ Donate coffee</a></td>
<td align="center"><a href="https://afdian.net/@LTCat"><img src="https://pic1.afdiancdn.com/user/df2dfad2960911e89c5252540025c377/avatar/31c78ee63d5dce8ac4848c837fb04204_w160_h160_s35.jpg?imageView2/1/w/240/h/240" width="64px;" alt=""/><br /><sub><b>龙腾猫跃</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">☕ Donate coffee</a></td>
</tr>
</table>
Expand Down
4 changes: 2 additions & 2 deletions src/data/VanillaData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ async function getSingleVanillaData(type: DataType, source: DataSource, version:
return cache[version]
}

export async function getVanillaData(versionOrLiteral: string, source: DataSource, versionInformation: VersionInformation | undefined, globalStoragePath: string) {
if (!versionInformation) {
export async function getVanillaData(versionOrLiteral: string | null, source: DataSource, versionInformation: VersionInformation | undefined, globalStoragePath: string) {
if (!versionInformation || !versionOrLiteral) {
return FallbackVanillaData
}
const ans: VanillaData = {} as any
Expand Down
43 changes: 24 additions & 19 deletions src/utils/handlers/onCodeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,36 @@ import { ErrorCode } from '../../types/ParsingError'
import { areOverlapped } from '../../types/TextRange'

export function onCodeAction({ uri, info, diagnostics, range }: { uri: Uri, info: FunctionInfo, diagnostics: Diagnostic[], range: Range, cacheFile: CacheFile }): CodeAction[] | null {
const ans: CodeAction[] = []
try {
const ans: CodeAction[] = []

for (let i = range.start.line; i <= range.end.line; i++) {
const line = info.lines[i]
const selectedRange = { start: range.start.character, end: range.end.character }

/* istanbul ignore else */
if (line) {
for (const { data } of line.args) {
if (data instanceof ArgumentNode) {
const nodeRange = data[NodeRange]
if (areOverlapped(selectedRange, nodeRange)) {
const diagnosticsMap: DiagnosticMap = {}
for (const diag of diagnostics) {
if (diag.code !== undefined) {
diagnosticsMap[diag.code as ErrorCode] = diagnosticsMap[diag.code as ErrorCode] || []
diagnosticsMap[diag.code as ErrorCode]!.push(diag)
for (let i = range.start.line; i <= range.end.line; i++) {
const line = info.lines[i]
const selectedRange = { start: range.start.character, end: range.end.character }

/* istanbul ignore else */
if (line) {
for (const { data } of line.args) {
if (data instanceof ArgumentNode) {
const nodeRange = data[NodeRange]
if (areOverlapped(selectedRange, nodeRange)) {
const diagnosticsMap: DiagnosticMap = {}
for (const diag of diagnostics) {
if (diag.code !== undefined) {
diagnosticsMap[diag.code as ErrorCode] = diagnosticsMap[diag.code as ErrorCode] || []
diagnosticsMap[diag.code as ErrorCode]!.push(diag)
}
}
ans.push(...data[GetCodeActions](uri.toString(), info, i, selectedRange, diagnosticsMap))
}
ans.push(...data[GetCodeActions](uri.toString(), info, i, selectedRange, diagnosticsMap))
}
}
}
}
}

return ans
return ans
} catch (e) {
console.error(e)
}
return null
}
25 changes: 15 additions & 10 deletions src/utils/handlers/onColorPresentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import { ColorPresentation } from 'vscode-languageserver'
import { FunctionInfo } from '../../types/FunctionInfo'

export function onColorPresentation({ info, lineNumber, start, end, r, g, b, a }: { info: FunctionInfo, start: number, end: number, lineNumber: number, r: number, g: number, b: number, a: number }) {
const ans: ColorPresentation[] = []
try {
const ans: ColorPresentation[] = []

const string = info.strings[lineNumber].slice(start, end)
if (string.startsWith('dust')) {
ans.push({ label: `dust ${r} ${g} ${b}` })
} else if (string.startsWith('minecraft:dust')) {
ans.push({ label: `minecraft:dust ${r} ${g} ${b}` })
} else {
ans.push({ label: `${(Math.round(r * 255) << 16) + (Math.round(g * 255) << 8) + Math.round(b * 255)}` })
}
const string = info.strings[lineNumber].slice(start, end)
if (string.startsWith('dust')) {
ans.push({ label: `dust ${r} ${g} ${b}` })
} else if (string.startsWith('minecraft:dust')) {
ans.push({ label: `minecraft:dust ${r} ${g} ${b}` })
} else {
ans.push({ label: `${(Math.round(r * 255) << 16) + (Math.round(g * 255) << 8) + Math.round(b * 255)}` })
}

return ans
return ans
} catch (e) {
console.error(e)
}
return null
}
2 changes: 1 addition & 1 deletion src/utils/handlers/onCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function onCompletion({ char, lineNumber, info, cacheFile, commandT

return completions
} catch (e) {
console.log(e)
console.error(e)
}
return null
}
53 changes: 29 additions & 24 deletions src/utils/handlers/onDocumentLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,41 @@ import { IdentityNode } from '../../nodes/IdentityNode'
import { getUriFromId } from '.'

export async function onDocumentLinks({ info, roots, uris, urisOfIds, pathExists }: { info: FunctionInfo, roots: Uri[], uris: UrisOfStrings, urisOfIds: UrisOfIds, pathExists: PathExistsFunction }) {
const ans: DocumentLink[] = []
try {
const ans: DocumentLink[] = []

for (let i = 0; i < info.lines.length; i++) {
const { cache } = info.lines[i]
for (const type in cache) {
if (isFileType(type)) {
const category = cache[type]
for (const id in category) {
/* istanbul ignore next */
if (category.hasOwnProperty(id)) {
const unit = category[id] as CacheUnit
const ref = [...unit.def, ...unit.ref]
for (const pos of ref) {
const link = {
range: {
start: { line: i, character: pos.start },
end: { line: i, character: pos.end }
},
target: await getUriFromId(pathExists, roots, uris, urisOfIds, IdentityNode.fromString(id), type)
}
/* istanbul ignore next */
if (link.target) {
ans.push({ range: link.range, target: link.target.toString() })
for (let i = 0; i < info.lines.length; i++) {
const { cache } = info.lines[i]
for (const type in cache) {
if (isFileType(type)) {
const category = cache[type]
for (const id in category) {
/* istanbul ignore next */
if (category.hasOwnProperty(id)) {
const unit = category[id] as CacheUnit
const ref = [...unit.def, ...unit.ref]
for (const pos of ref) {
const link = {
range: {
start: { line: i, character: pos.start },
end: { line: i, character: pos.end }
},
target: await getUriFromId(pathExists, roots, uris, urisOfIds, IdentityNode.fromString(id), type)
}
/* istanbul ignore next */
if (link.target) {
ans.push({ range: link.range, target: link.target.toString() })
}
}
}
}
}
}
}
}

return ans
return ans
} catch (e) {
console.error(e)
}
return null
}
89 changes: 47 additions & 42 deletions src/utils/handlers/onFoldingRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,63 @@ function getCommentSymbolAmount(string: string) {
}

export function onFoldingRanges({ info }: { info: FunctionInfo }) {
const ans: FoldingRange[] = []
try {
const ans: FoldingRange[] = []

const regionStartLineNumbers: number[] = []
const commentStartLineNumers: { [level: number]: number | undefined } = {}
let i = 0
for (const string of info.strings) {
if (string.startsWith('#region')) {
regionStartLineNumbers.push(i)
} else if (string.startsWith('#endregion')) {
const startLineNumber = regionStartLineNumbers.pop()
if (startLineNumber !== undefined) {
// End ‘#region’s.
ans.push(FoldingRange.create(
startLineNumber, i,
undefined, undefined, FoldingRangeKind.Region
))
}
for (const levelString in commentStartLineNumers) {
// End normal comments.
const level = parseFloat(levelString)
ans.push(FoldingRange.create(
commentStartLineNumers[level]!, i - 1,
undefined, undefined, FoldingRangeKind.Region
))
delete commentStartLineNumers[level]
}
} else {
const amount = getCommentSymbolAmount(string)
for (const levelString in commentStartLineNumers) {
const level = parseFloat(levelString)
if (amount > 0 && level >= amount && commentStartLineNumers[level] !== undefined) {
// End equal-or-lower-level comments.
const regionStartLineNumbers: number[] = []
const commentStartLineNumers: { [level: number]: number | undefined } = {}
let i = 0
for (const string of info.strings) {
if (string.startsWith('#region')) {
regionStartLineNumbers.push(i)
} else if (string.startsWith('#endregion')) {
const startLineNumber = regionStartLineNumbers.pop()
if (startLineNumber !== undefined) {
// End ‘#region’s.
ans.push(FoldingRange.create(
commentStartLineNumers[level]!, i - 1,
startLineNumber, i,
undefined, undefined, FoldingRangeKind.Region
))
delete commentStartLineNumers[level]
} else if (i === info.strings.length - 1) {
// End normal comments at end of file.
}
for (const levelString in commentStartLineNumers) {
// End normal comments.
const level = parseFloat(levelString)
ans.push(FoldingRange.create(
commentStartLineNumers[level]!, i,
commentStartLineNumers[level]!, i - 1,
undefined, undefined, FoldingRangeKind.Region
))
delete commentStartLineNumers[level]
}
} else {
const amount = getCommentSymbolAmount(string)
for (const levelString in commentStartLineNumers) {
const level = parseFloat(levelString)
if (amount > 0 && level >= amount && commentStartLineNumers[level] !== undefined) {
// End equal-or-lower-level comments.
ans.push(FoldingRange.create(
commentStartLineNumers[level]!, i - 1,
undefined, undefined, FoldingRangeKind.Region
))
delete commentStartLineNumers[level]
} else if (i === info.strings.length - 1) {
// End normal comments at end of file.
ans.push(FoldingRange.create(
commentStartLineNumers[level]!, i,
undefined, undefined, FoldingRangeKind.Region
))
delete commentStartLineNumers[level]
}
}
if (amount > 0) {
commentStartLineNumers[amount] = i
}
}
if (amount > 0) {
commentStartLineNumers[amount] = i
}
i += 1
}
i += 1
}

return ans
return ans
} catch (e) {
console.error(e)
}
return null
}
Loading

0 comments on commit b12061d

Please sign in to comment.