|
| 1 | +import { ClasslikeInfo } from 'common/php/ClasslikeInfo'; |
| 2 | +import PhpDocumentParser from 'common/php/PhpDocumentParser'; |
| 3 | +import PhpNamespace from 'common/PhpNamespace'; |
| 4 | +import { AutoloadNamespaceIndexData } from 'indexer/autoload-namespace/AutoloadNamespaceIndexData'; |
| 5 | +import AutoloadNamespaceIndexer from 'indexer/autoload-namespace/AutoloadNamespaceIndexer'; |
| 6 | +import IndexManager from 'indexer/IndexManager'; |
| 7 | +import { |
| 8 | + DefinitionProvider, |
| 9 | + TextDocument, |
| 10 | + Position, |
| 11 | + CancellationToken, |
| 12 | + LocationLink, |
| 13 | + Uri, |
| 14 | + Range, |
| 15 | +} from 'vscode'; |
| 16 | + |
| 17 | +export class XmlClasslikeDefinitionProvider implements DefinitionProvider { |
| 18 | + private namespaceIndexData: AutoloadNamespaceIndexData | undefined; |
| 19 | + |
| 20 | + public async provideDefinition( |
| 21 | + document: TextDocument, |
| 22 | + position: Position, |
| 23 | + token: CancellationToken |
| 24 | + ) { |
| 25 | + const range = document.getWordRangeAtPosition(position, /"[^"]*"/); |
| 26 | + |
| 27 | + if (!range) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + const word = document.getText(range); |
| 32 | + |
| 33 | + const namespaceIndexData = this.getNamespaceIndexData(); |
| 34 | + |
| 35 | + if (!namespaceIndexData) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + const potentialNamespace = word.replace(/"/g, ''); |
| 40 | + |
| 41 | + const classUri = await namespaceIndexData.findClassByNamespace( |
| 42 | + PhpNamespace.fromString(potentialNamespace) |
| 43 | + ); |
| 44 | + |
| 45 | + if (!classUri) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + const targetPosition = await this.getClasslikeNameRange(document, classUri); |
| 50 | + |
| 51 | + return [ |
| 52 | + { |
| 53 | + targetUri: classUri, |
| 54 | + targetRange: targetPosition, |
| 55 | + originSelectionRange: range, |
| 56 | + } as LocationLink, |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + private getNamespaceIndexData(): AutoloadNamespaceIndexData | undefined { |
| 61 | + if (!this.namespaceIndexData) { |
| 62 | + const namespaceIndex = IndexManager.getIndexData(AutoloadNamespaceIndexer.KEY); |
| 63 | + |
| 64 | + if (!namespaceIndex) { |
| 65 | + return undefined; |
| 66 | + } |
| 67 | + |
| 68 | + this.namespaceIndexData = new AutoloadNamespaceIndexData(namespaceIndex); |
| 69 | + } |
| 70 | + |
| 71 | + return this.namespaceIndexData; |
| 72 | + } |
| 73 | + |
| 74 | + private async getClasslikeNameRange( |
| 75 | + document: TextDocument, |
| 76 | + classUri: Uri |
| 77 | + ): Promise<Position | Range> { |
| 78 | + const phpFile = await PhpDocumentParser.parseUri(document, classUri); |
| 79 | + const classLikeInfo = new ClasslikeInfo(phpFile); |
| 80 | + const range = classLikeInfo.getNameRange(); |
| 81 | + |
| 82 | + if (!range) { |
| 83 | + return new Position(0, 0); |
| 84 | + } |
| 85 | + |
| 86 | + return range; |
| 87 | + } |
| 88 | +} |
0 commit comments