mirror of
https://github.com/Strum355/mcshader-lsp.git
synced 2025-07-23 11:15:27 +00:00
Fixed line numbers for diagnostics. Also added a starter graph class so that i can traverse the include tree
This commit is contained in:
parent
6a484de93e
commit
e2d7f8ea3d
2 changed files with 35 additions and 2 deletions
26
server/src/graph.ts
Normal file
26
server/src/graph.ts
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
type Node = {
|
||||||
|
parents: Map<string, Node>
|
||||||
|
children: Map<string, Node>
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Graph {
|
||||||
|
public nodes: Map<string, Node> = new Map()
|
||||||
|
|
||||||
|
public hasParents(uri: string): boolean {
|
||||||
|
return this.nodes.has(uri) ? this.nodes.get(uri).parents.size > 0 : false
|
||||||
|
}
|
||||||
|
|
||||||
|
public setParent(uri: string, parent: string) {
|
||||||
|
const par: Node = this.nodes.has(parent) ? this.nodes.get(parent) : {parents: new Map(), children: new Map()}
|
||||||
|
if (this.nodes.has(uri)) {
|
||||||
|
const node = this.nodes.get(uri)
|
||||||
|
node.parents.set(parent, par)
|
||||||
|
par.children.set(uri, node)
|
||||||
|
} else {
|
||||||
|
const node: Node = {parents: new Map([par].map(p => [parent, p]) as [string, Node][]), children: new Map()}
|
||||||
|
par.children.set(uri, node)
|
||||||
|
this.nodes.set(uri, node)
|
||||||
|
}
|
||||||
|
this.nodes.set(parent, par)
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import { readFileSync, existsSync } from 'fs'
|
||||||
import { conf } from './config'
|
import { conf } from './config'
|
||||||
import { postError, formatURI, getDocumentContents } from './utils'
|
import { postError, formatURI, getDocumentContents } from './utils'
|
||||||
import { platform } from 'os'
|
import { platform } from 'os'
|
||||||
|
import { Graph } from './graph'
|
||||||
|
|
||||||
const reDiag = /^(ERROR|WARNING): ([^?<>*|"]+?):(\d+): (?:'.*?' : )?(.+)\r?/
|
const reDiag = /^(ERROR|WARNING): ([^?<>*|"]+?):(\d+): (?:'.*?' : )?(.+)\r?/
|
||||||
const reVersion = /#version [\d]{3}/
|
const reVersion = /#version [\d]{3}/
|
||||||
|
@ -21,7 +22,10 @@ const filters = [
|
||||||
/Could not process include directive for header name:/
|
/Could not process include directive for header name:/
|
||||||
]
|
]
|
||||||
|
|
||||||
|
export const includeGraph = new Graph()
|
||||||
|
|
||||||
export const includeToParent = new Map<string, Set<string>>()
|
export const includeToParent = new Map<string, Set<string>>()
|
||||||
|
export const allFiles = new Set<string>()
|
||||||
|
|
||||||
type IncludeObj = {
|
type IncludeObj = {
|
||||||
lineNum: number,
|
lineNum: number,
|
||||||
|
@ -101,6 +105,9 @@ export function preprocess(lines: string[], docURI: string) {
|
||||||
|
|
||||||
processIncludes(lines, [docURI], allIncludes, diagnostics, hasDirective)
|
processIncludes(lines, [docURI], allIncludes, diagnostics, hasDirective)
|
||||||
|
|
||||||
|
allIncludes.forEach(inc => allFiles.add(inc.path))
|
||||||
|
console.log(JSON.stringify(allIncludes, null, 2))
|
||||||
|
|
||||||
const includeMap = new Map<string, IncludeObj>(allIncludes.map(obj => [obj.path, obj]) as [string, IncludeObj][])
|
const includeMap = new Map<string, IncludeObj>(allIncludes.map(obj => [obj.path, obj]) as [string, IncludeObj][])
|
||||||
|
|
||||||
lint(docURI, lines, includeMap, diagnostics)
|
lint(docURI, lines, includeMap, diagnostics)
|
||||||
|
@ -136,7 +143,7 @@ export function getIncludes(uri: string, lines: string[]) {
|
||||||
comment = isInComment(line, comment)
|
comment = isInComment(line, comment)
|
||||||
count[count.length - 1]++
|
count[count.length - 1]++
|
||||||
total++
|
total++
|
||||||
if (comment) return out
|
//if (comment) return out
|
||||||
if (line.startsWith('#line')) {
|
if (line.startsWith('#line')) {
|
||||||
const inc = line.slice(line.indexOf('"') + 1, line.lastIndexOf('"'))
|
const inc = line.slice(line.indexOf('"') + 1, line.lastIndexOf('"'))
|
||||||
|
|
||||||
|
@ -198,7 +205,7 @@ function mergeInclude(inc: IncludeObj, lines: string[], incStack: string[], diag
|
||||||
// TODO do we wanna use a DLL here?
|
// TODO do we wanna use a DLL here?
|
||||||
lines.splice(inc.lineNumTopLevel + 1, 0, ...dataLines)
|
lines.splice(inc.lineNumTopLevel + 1, 0, ...dataLines)
|
||||||
// add the closing #line indicating we're re-entering a block a level up
|
// add the closing #line indicating we're re-entering a block a level up
|
||||||
lines.splice(inc.lineNumTopLevel + 1 + dataLines.length, 0, `#line ${inc.lineNum} "${inc.parent}"`)
|
lines.splice(inc.lineNumTopLevel + 1 + dataLines.length, 0, `#line ${inc.lineNum + 1} "${inc.parent}"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
function lint(uri: string, lines: string[], includes: Map<string, IncludeObj>, diagnostics: Map<string, Diagnostic[]>) {
|
function lint(uri: string, lines: string[], includes: Map<string, IncludeObj>, diagnostics: Map<string, Diagnostic[]>) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue