Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #78 from zzzz465/feature/onhover
Browse files Browse the repository at this point in the history
feature: show data on defReference hover
  • Loading branch information
zzzz465 authored Feb 17, 2022
2 parents 7dbcaca + 10ba500 commit 3e601b6
Show file tree
Hide file tree
Showing 14 changed files with 435 additions and 51 deletions.
1 change: 1 addition & 0 deletions analyzer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"linq-es2015": "^2.5.1",
"lodash": "^4.17.21",
"normalize-path": "^3.0.0",
"prettydiff": "^101.2.6",
"typescript-collections": "^1.3.3",
"vscode-uri": "^3.0.2"
}
Expand Down
22 changes: 21 additions & 1 deletion analyzer/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions analyzer/src/parser/domhandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export class DomHandler {

const elem = this.tagStack.pop() as Element
elem.nodeRange.end = this.parser.endIndex
elem.closeTagNameRange.start = this.parser.tagNameStartIndex
elem.closeTagNameRange.end = this.parser.tagNameEndIndex
elem.closeTagRange.start = this.parser.startIndex
elem.closeTagRange.end = this.parser.endIndex

if (this.elementCB) this.elementCB(elem)
}
Expand Down
36 changes: 36 additions & 0 deletions analyzer/src/parser/domhandler/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export class Node {
cloneNode<T extends Node>(this: T, recursive = false): T {
return cloneNode(this, recursive)
}

toString(): string {
throw new Error('toString not implemented')
}
}

/**
Expand Down Expand Up @@ -129,6 +133,10 @@ export class DataNode extends Node {
findNodeAt(offset: number): RangedNode | undefined {
return findNodeAt(this, offset)
}

toString(): string {
return this.data
}
}

/**
Expand All @@ -147,6 +155,11 @@ export class Comment extends DataNode {
constructor(data: string) {
super(ElementType.Comment, data)
}

toString(): string {
// TODO: should I return this with <!-- data --> ??
return this.data
}
}

/**
Expand Down Expand Up @@ -193,6 +206,10 @@ export class NodeWithChildren extends Node {
set childNodes(children: Node[]) {
this.children = children
}

toString(): string {
return this.childNodes.map((node) => node.toString()).join('')
}
}

/**
Expand Down Expand Up @@ -227,6 +244,10 @@ export class Document extends NodeWithChildren {
}

'x-mode'?: 'no-quirks' | 'quirks' | 'limited-quirks'

toString(): string {
return this.rawText
}
}

/**
Expand Down Expand Up @@ -335,6 +356,12 @@ export class Element extends NodeWithChildren {

'x-attribsNamespace'?: Record<string, string>
'x-attribsPrefix'?: Record<string, string>

toString(): string {
const openTagString = this.document.rawText.slice(this.openTagRange.start, this.openTagRange.end)
const closeTagString = this.document.rawText.slice(this.closeTagRange.start, this.closeTagRange.end)
return `${openTagString}${super.toString()}${closeTagString}`
}
}

/**
Expand Down Expand Up @@ -412,6 +439,12 @@ export function cloneNode<T extends Node>(node: T, recursive = false): T {
const clone = new Element(node.name, { ...node.attribs }, children)
children.forEach((child) => (child.parent = clone))

Object.assign(clone.nodeRange, node.nodeRange)
Object.assign(clone.openTagRange, node.openTagRange)
Object.assign(clone.openTagNameRange, node.openTagNameRange)
Object.assign(clone.closeTagRange, node.closeTagRange)
Object.assign(clone.closeTagNameRange, node.closeTagNameRange)

if (node['x-attribsNamespace']) {
clone['x-attribsNamespace'] = { ...node['x-attribsNamespace'] }
}
Expand Down Expand Up @@ -449,6 +482,9 @@ export function cloneNode<T extends Node>(node: T, recursive = false): T {
throw new Error(`Not implemented yet: ${node.type}`)
}

result.parent = node.parent
result.prev = node.prev
result.next = node.next
result.startIndex = node.startIndex
result.endIndex = node.endIndex
return result as T
Expand Down
58 changes: 58 additions & 0 deletions analyzer/src/test/tostring/tostring.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { parse } from "../.."
import 'prettydiff'

const exampleXML = `
<?xml version="1.0" encoding="utf-8" ?>
<Defs>
<SoundDef>
<defName>Interact_Drill</defName>
<sustain>True</sustain>
<context>MapOnly</context>
<maxSimultaneous>1</maxSimultaneous>
<priorityMode>PrioritizeNearest</priorityMode>
<subSounds>
<li>
<muteWhenPaused>True</muteWhenPaused>
<grains>
<li Class="AudioGrain_Clip">
<clipPath>Interact/Work/Drill/Drill_loop</clipPath>
</li>
</grains>
<volumeRange>6~6</volumeRange>
<pitchRange>0.93~1.07</pitchRange>
<distRange>25~40</distRange>
<sustainAttack>1.1</sustainAttack>
</li>
</subSounds>
</SoundDef>
</Defs>
`

describe('XML toString() test', () => {
test('toString() result must be parsable xml', () => {
const root = parse(exampleXML)

const soundDefs = root.findNode((node) => node.tagName === 'SoundDef')
expect(soundDefs.length).toBe(1)

const soundDef = soundDefs[0]

const defNameNodes = soundDef.findNode((node) => node.tagName === 'defName')
expect(defNameNodes.length).toBe(1)

const defNameNode = defNameNodes[0]
const defNameNodeIndex = soundDef.childNodes.indexOf(defNameNode)
expect(defNameNodeIndex).toBeLessThan(soundDef.childNodes.length)

const otherNodesToAdd = soundDef.childNodes.slice(defNameNodeIndex + 1)

const node = soundDef.cloneNode()
node.children = [defNameNode, ...otherNodesToAdd]

const result = node.toString()

console.log(result)
})
})
1 change: 1 addition & 0 deletions language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"linq-es2015": "^2.5.1",
"lodash": "^4.17.21",
"normalize-path": "^3.0.0",
"prettydiff": "^101.2.6",
"reflect-metadata": "^0.1.13",
"tsconfig-paths": "^3.9.0",
"tsyringe": "^4.6.0",
Expand Down
22 changes: 21 additions & 1 deletion language-server/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion language-server/src/dependencyResourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import EventEmitter from 'events'
import { inject, singleton } from 'tsyringe'
import { Connection } from 'vscode-languageserver'
import { ConnectionToken } from './connection'
import { File } from './fs'
import { DependencyFile, File } from './fs'
import { About, Dependency } from './mod'
import { NotificationEvents } from './notificationEventManager'
import * as winston from 'winston'
Expand Down
Loading

0 comments on commit 3e601b6

Please sign in to comment.