Skip to content

Commit

Permalink
Add type checker
Browse files Browse the repository at this point in the history
  • Loading branch information
cooolbros committed Dec 22, 2021
1 parent 3cdb259 commit 645c828
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
3 changes: 2 additions & 1 deletion servers/vdf/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { getVDFFormatDocumentSymbols, printVDFFormatDocumentSymbols } from "./fo
import { hudTypes } from "./HUD/keys";
import { statichudKeyBitValues, statichudKeyValues } from "./HUD/values";
import clientscheme from "./JSON/clientscheme.json";
import { validate } from "./validator";
import { VDFExtended } from "./vdf_extended";


Expand Down Expand Up @@ -81,7 +82,7 @@ documents.onDidChangeContent((change: TextDocumentChangeEvent<TextDocument>): vo
try {
const documentSymbols = getVDFDocumentSymbols(change.document.getText())
documentsSymbols[change.document.uri] = documentSymbols
return []
return validate(documentsSymbols[change.document.uri])
}
catch (e: unknown) {
if (e instanceof VDFSyntaxError) {
Expand Down
52 changes: 52 additions & 0 deletions servers/vdf/src/validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Diagnostic, DiagnosticSeverity } from "vscode-languageserver";
import { VDFDocumentSymbol } from "../../../shared/tools/src/tools";
import { VDF } from "../../../shared/vdf";

export function validate(documentSymbols: VDFDocumentSymbol[]): Diagnostic[] {
const diagnostics: Diagnostic[] = []

const addDiagnostics = (objectPath: string[], _documentSymbols: VDFDocumentSymbol[]): void => {
for (const { name, key, value, children, valueRange } of _documentSymbols) {
if (value && valueRange) {
let _key = name.toLowerCase()
const _value = value.toLowerCase()
switch (_key) {
case "fieldname":
{
// fieldName must match element name
const elementName = objectPath[objectPath.length - 1].split(VDF.OSTagDelimeter)[0].toLowerCase()
if (elementName != _value) {
diagnostics.push({
message: `fieldName "${value}" does not match element name "${elementName}"`,
range: valueRange,
severity: DiagnosticSeverity.Warning,
})
}
break
}
case "pin_to_sibling":
{
// Element should not be pinned to itself
if (_value == key.split(VDF.OSTagDelimeter)[0]) {
diagnostics.push({
message: `Element "${objectPath[objectPath.length - 1]}" is pinned to itself!`,
range: valueRange,
severity: DiagnosticSeverity.Warning,
})
}
break
}
}
}
else if (children) {
objectPath.push(key)
addDiagnostics(objectPath, children)
objectPath.pop()
}
}
}

addDiagnostics([], documentSymbols)

return diagnostics
}
16 changes: 12 additions & 4 deletions shared/tools/src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function loadAllControls(filePath: string): any {
}
export interface VDFDocumentSymbol extends DocumentSymbol {
name: string
key: string
nameRange: Range
value?: string
valueRange?: Range,
Expand Down Expand Up @@ -110,7 +111,8 @@ export function getVDFDocumentSymbols(str: string, options?: VDFTokeniserOptions
keyRange = Range.create(tokeniser.line, tokeniser.character - tokeniser.quoted - key.length, tokeniser.line, tokeniser.character - tokeniser.quoted)

if (value.startsWith("[") && value.endsWith("]") && (tokeniser.options.osTags == VDFOSTags.Objects || tokeniser.options.osTags == VDFOSTags.All)) {
key += ` ${tokeniser.next()}`
const osTag = tokeniser.next()

tokeniser.next() // Skip opening brace

const children = parseObject(true)
Expand All @@ -119,7 +121,8 @@ export function getVDFDocumentSymbols(str: string, options?: VDFTokeniserOptions
const selectionRange = Range.create(keyRange.start, endPosition)

documentSymbols.push({
name: key,
name: `${key} ${osTag}`,
key: `${key}${VDF.OSTagDelimeter}${osTag}`,
nameRange: keyRange,
range: selectionRange,
selectionRange: selectionRange,
Expand All @@ -136,6 +139,7 @@ export function getVDFDocumentSymbols(str: string, options?: VDFTokeniserOptions

documentSymbols.push({
name: key,
key: key,
nameRange: keyRange,
range: selectionRange,
selectionRange: selectionRange,
Expand All @@ -150,18 +154,22 @@ export function getVDFDocumentSymbols(str: string, options?: VDFTokeniserOptions
throw new VDFSyntaxError(`Value expected for "${key}"!`, keyRange)
}

let _key = key

const lookahead: string = tokeniser.next(true)
if (lookahead.startsWith("[") && lookahead.endsWith("]") && (tokeniser.options.osTags == VDFOSTags.Strings || tokeniser.options.osTags == VDFOSTags.All)) {
key += ` ${tokeniser.next()}`
const osTag = tokeniser.next()
key += ` ${osTag}`
_key += `${VDF.OSTagDelimeter}${osTag}`
}

const valueRange = Range.create(Position.create(tokeniser.line, tokeniser.character - tokeniser.quoted - value.length), Position.create(tokeniser.line, tokeniser.character - tokeniser.quoted))

const containingRange = Range.create(keyRange.start, valueRange.end)


documentSymbols.push({
name: key,
key: _key,
nameRange: keyRange,
range: containingRange,
selectionRange: containingRange,
Expand Down
2 changes: 1 addition & 1 deletion shared/vdf/src/vdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class VDFSyntaxError extends Error {
}

export class VDF {
static readonly OSTagDelimeter: string = "^"
static readonly OSTagDelimeter: "^" = "^"
static parse(str: string, options?: VDFTokeniserOptions) {
const tokeniser = new VDFTokeniser(str, options)
const parseObject = (): { [key: string]: any } => {
Expand Down

0 comments on commit 645c828

Please sign in to comment.