-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
provideReferences
in plugins (#1066)
* Add support for `provideReferences` in plugins * Add plugin docs updates
- Loading branch information
1 parent
074877a
commit ee1d7f9
Showing
7 changed files
with
221 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { expect } from '../../chai-config.spec'; | ||
import { Program } from '../../Program'; | ||
import { standardizePath as s, util } from '../../util'; | ||
let rootDir = s`${process.cwd()}/rootDir`; | ||
import { createSandbox } from 'sinon'; | ||
import { ReferencesProvider } from './ReferencesProvider'; | ||
import type { Location } from 'vscode-languageserver-protocol'; | ||
import { URI } from 'vscode-uri'; | ||
const sinon = createSandbox(); | ||
|
||
describe('ReferencesProvider', () => { | ||
let program: Program; | ||
beforeEach(() => { | ||
program = new Program({ | ||
rootDir: rootDir | ||
}); | ||
sinon.restore(); | ||
}); | ||
|
||
afterEach(() => { | ||
program.dispose(); | ||
sinon.restore(); | ||
}); | ||
|
||
it('handles unknown file type', () => { | ||
const result = new ReferencesProvider({ | ||
program: program, | ||
file: undefined, | ||
position: util.createPosition(1, 2), | ||
references: [] | ||
}).process(); | ||
expect(result).to.eql([]); | ||
}); | ||
|
||
it('finds references for variables in same function', () => { | ||
const file = program.setFile('source/main.brs', ` | ||
sub main() | ||
name = "John" | ||
print name | ||
name = name + " Doe" | ||
end sub | ||
`); | ||
expect( | ||
util.sortByRange( | ||
program.getReferences('source/main.brs', util.createPosition(3, 25)) | ||
).map(locationToString) | ||
).to.eql([ | ||
s`${file.srcPath}:2:16-2:20`, | ||
s`${file.srcPath}:3:22-3:26`, | ||
s`${file.srcPath}:4:16-4:20`, | ||
s`${file.srcPath}:4:23-4:27` | ||
]); | ||
}); | ||
|
||
function locationToString(loc: Location) { | ||
return `${URI.parse(loc.uri).fsPath}:${loc.range.start.line}:${loc.range.start.character}-${loc.range.end.line}:${loc.range.end.character}`; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { BrsFile } from '../../files/BrsFile'; | ||
import type { ProvideReferencesEvent } from '../../interfaces'; | ||
import type { Location } from 'vscode-languageserver-protocol'; | ||
import util from '../../util'; | ||
import { WalkMode, createVisitor } from '../../astUtils/visitors'; | ||
import type { XmlFile } from '../../files/XmlFile'; | ||
import { isBrsFile, isXmlFile } from '../../astUtils/reflection'; | ||
|
||
export class ReferencesProvider { | ||
constructor( | ||
private event: ProvideReferencesEvent | ||
) { } | ||
|
||
public process(): Location[] { | ||
if (isBrsFile(this.event.file)) { | ||
this.brsFileGetReferences(this.event.file); | ||
} else if (isXmlFile(this.event.file)) { | ||
this.xmlFileGetReferences(this.event.file); | ||
} | ||
return this.event.references; | ||
} | ||
|
||
/** | ||
* For a position in a BrsFile, get the location where the token at that position was defined | ||
*/ | ||
private brsFileGetReferences(file: BrsFile): void { | ||
|
||
const callSiteToken = file.getTokenAt(this.event.position); | ||
|
||
const searchFor = callSiteToken.text.toLowerCase(); | ||
|
||
const scopes = this.event.program.getScopesForFile(file); | ||
|
||
for (const scope of scopes) { | ||
const processedFiles = new Set<BrsFile>(); | ||
for (const file of scope.getAllFiles()) { | ||
if (!isBrsFile(file) || processedFiles.has(file)) { | ||
continue; | ||
} | ||
processedFiles.add(file); | ||
file.ast.walk(createVisitor({ | ||
AssignmentStatement: (s) => { | ||
if (s.name?.text?.toLowerCase() === searchFor) { | ||
this.event.references.push(util.createLocation(util.pathToUri(file.srcPath), s.name.range)); | ||
} | ||
}, | ||
VariableExpression: (e) => { | ||
if (e.name.text.toLowerCase() === searchFor) { | ||
this.event.references.push(util.createLocation(util.pathToUri(file.srcPath), e.range)); | ||
} | ||
} | ||
}), { | ||
walkMode: WalkMode.visitAllRecursive | ||
}); | ||
} | ||
} | ||
} | ||
|
||
private xmlFileGetReferences(file: XmlFile) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters