• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rokucommunity / vscode-brightscript-language / #2719

24 Aug 2022 12:51PM UTC coverage: 41.691% (+0.02%) from 41.675%
#2719

push

TwitchBronBron
2.35.0

477 of 1427 branches covered (33.43%)

Branch coverage included in aggregate %.

1126 of 2418 relevant lines covered (46.57%)

7.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

6.8
/src/DefinitionRepository.ts
1
import * as vscode from 'vscode';
1✔
2
import type {
3
    Location,
4
    Position,
5
    TextDocument
6
} from 'vscode';
7
import { Uri } from 'vscode';
8
import { BrightScriptDeclaration } from './BrightScriptDeclaration';
1✔
9
import type { DeclarationProvider } from './DeclarationProvider';
10
import { getExcludeGlob } from './DeclarationProvider';
1✔
11

12
export class DefinitionRepository {
1✔
13

14
    constructor(
15
        public provider: DeclarationProvider
21✔
16
    ) {
17
        this.declarationProvider = provider;
21✔
18
        provider.onDidChange((e) => {
21✔
19
            this.cache.set(e.uri.fsPath, e.decls.filter((d) => d.isGlobal));
×
20
        });
21
        provider.onDidDelete((e) => {
21✔
22
            this.cache.delete(e.uri.fsPath);
×
23
        });
24
        provider.onDidReset((e) => {
21✔
25
            this.cache.clear();
×
26
        });
27
    }
28

29
    private declarationProvider: DeclarationProvider;
30
    private cache: Map<string, BrightScriptDeclaration[]> = new Map();
21✔
31

32
    public sync(): Promise<void> {
33
        return this.provider.sync();
×
34
    }
35

36
    public *find(document: TextDocument, position: Position): IterableIterator<Location> {
37
        const word = this.getWord(document, position).toLowerCase(); //brightscript is not case sensitive!
×
38

39
        void this.sync();
×
40
        if (word === undefined) {
×
41
            return;
×
42
        }
43
        yield* this.findInCurrentDocument(document, position, word);
×
44
        const ws = vscode.workspace.getWorkspaceFolder(document.uri);
×
45
        if (ws === undefined) {
×
46
            return;
×
47
        }
48
        const fresh: Set<string> = new Set([document.uri.fsPath]);
×
49
        for (const doc of vscode.workspace.textDocuments) {
×
50
            if (!doc.isDirty) {
×
51
                continue;
×
52
            }
53
            if (doc === document) {
×
54
                continue;
×
55
            }
56
            if (!this.cache.has(doc.uri.fsPath)) {
×
57
                continue;
×
58
            }
59
            if (!doc.uri.fsPath.startsWith(ws.uri.fsPath)) {
×
60
                continue;
×
61
            }
62
            fresh.add(doc.uri.fsPath);
×
63
            yield* this.findInDocument(doc, word);
×
64
        }
65
        for (const [path, defs] of this.cache.entries()) {
×
66
            if (fresh.has(path)) {
×
67
                continue;
×
68
            }
69
            if (!path.startsWith(ws.uri.fsPath)) {
×
70
                continue;
×
71
            }
72
            yield* defs.filter((d) => d.name.toLowerCase() === word).map((d) => d.getLocation());
×
73
        }
74
    }
75

76
    private getWord(document: TextDocument, position: Position): string {
77
        const wordRange = document.getWordRangeAtPosition(position, /[^\s\x21-\x2f\x3a-\x40\x5b-\x5e\x7b-\x7e]+/);
×
78

79
        const phraseRange = document.getWordRangeAtPosition(position, /(\w|\.)+/);
×
80
        if (wordRange !== undefined) {
×
81
            const word = document.getText(wordRange);
×
82
            if (phraseRange !== undefined) {
×
83
                const phrase = document.getText(phraseRange);
×
84
                let parts = phrase.split('.');
×
85
                const index = parts.indexOf(word);
×
86
                if (index < parts.length - 1) {
×
87
                    parts.splice(index + 1, parts.length - 1 - index);
×
88
                    return parts.join('.');
×
89
                }
90
            }
91
            return word;
×
92
        }
93
    }
94

95
    private findInCurrentDocument(document: TextDocument, position: Position, word: string): Location[] {
96
        return this.declarationProvider.readDeclarations(document.uri, document.getText())
×
97
            .filter((d) => {
98
                return d.name.toLowerCase() === word && d.visible(position);
×
99
            })
100
            .map((d) => {
101
                return d.getLocation();
×
102
            });
103
    }
104

105
    private findInDocument(document: TextDocument, word: string): Location[] {
106
        return this.declarationProvider.readDeclarations(document.uri, document.getText())
×
107
            .filter((d) => d.name.toLowerCase() === word && d.isGlobal)
×
108
            .map((d) => d.getLocation());
×
109
    }
110

111
    public *findDefinition(document: TextDocument, position: Position): IterableIterator<BrightScriptDeclaration> {
112
        const word = this.getWord(document, position).toLowerCase(); //brightscript is not case sensitive!
×
113
        let result = yield* this.findDefinitionForWord(document, position, word);
×
114
        return result;
×
115
    }
116
    // duplicating some of thisactivate.olympicchanel.com to reduce the risk of introducing nasty performance issues/unwanted behaviour by extending Location
117
    public *findDefinitionForWord(document: TextDocument, position: Position, word: string): IterableIterator<BrightScriptDeclaration> {
118

119
        void this.sync();
×
120
        if (word === undefined) {
×
121
            return;
×
122
        }
123
        yield* this.findDefinitionInCurrentDocument(document, position, word);
×
124
        const ws = vscode.workspace.getWorkspaceFolder(document.uri);
×
125
        if (ws === undefined) {
×
126
            return;
×
127
        }
128
        const fresh: Set<string> = new Set([document.uri.fsPath]);
×
129
        for (const doc of vscode.workspace.textDocuments) {
×
130
            console.log('>>>>>doc ' + doc.uri.path);
×
131

132
            if (!doc.isDirty) {
×
133
                continue;
×
134
            }
135
            if (doc === document) {
×
136
                continue;
×
137
            }
138
            if (!this.cache.has(doc.uri.fsPath)) {
×
139
                continue;
×
140
            }
141
            if (!doc.uri.fsPath.startsWith(ws.uri.fsPath)) {
×
142
                continue;
×
143
            }
144
            fresh.add(doc.uri.fsPath);
×
145
            yield* this.findDefinitionInDocument(doc, word);
×
146
        }
147
        for (const [path, defs] of this.cache.entries()) {
×
148
            if (fresh.has(path)) {
×
149
                continue;
×
150
            }
151
            if (!path.startsWith(ws.uri.fsPath)) {
×
152
                continue;
×
153
            }
154
            yield* defs.filter((d) => d.name.toLowerCase() === word);
×
155
        }
156
    }
157

158
    private findDefinitionInCurrentDocument(document: TextDocument, position: Position, word: string): BrightScriptDeclaration[] {
159
        return this.declarationProvider.readDeclarations(document.uri, document.getText())
×
160
            .filter((d) => d.name.toLowerCase() === word && d.visible(position));
×
161
    }
162

163
    public findDefinitionInDocument(document: TextDocument, word: string): BrightScriptDeclaration[] {
164
        return this.declarationProvider.readDeclarations(document.uri, document.getText())
×
165
            .filter((d) => d.name.toLowerCase() === word && d.isGlobal);
×
166
    }
167

168
    public async findDefinitionForBrsDocument(name: string): Promise<BrightScriptDeclaration[]> {
169
        let declarations = [];
×
170
        await this.sync();
×
171
        const excludes = getExcludeGlob();
×
172
        //get usable bit of name
173
        let fileName = name.replace(/^.*[\\\/]/, '').toLowerCase();
×
174
        for (const uri of await vscode.workspace.findFiles('**/*.{brs,bs}', excludes)) {
×
175
            if (uri.path.toLowerCase().includes(fileName)) {
×
176
                declarations.push(BrightScriptDeclaration.fromUri(uri));
×
177
            }
178
        }
179
        return declarations;
×
180
    }
181
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc