Skip to content

Commit 67c0dee

Browse files
wip: indexing
1 parent cd8e2c6 commit 67c0dee

File tree

10 files changed

+69
-33
lines changed

10 files changed

+69
-33
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
"type": "boolean",
6868
"default": true,
6969
"description": "Enable hover decorations for Magento 2 XML files."
70+
},
71+
"magento-toolbox.indexingBatchSize": {
72+
"type": "number",
73+
"default": 25,
74+
"description": "Number of files to index in a single batch."
7075
}
7176
}
7277
},

src/common/Config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { workspace } from 'vscode';
33
class Config {
44
public readonly SECTION = 'magento-toolbox';
55

6-
public get<T = string>(key: string): T | undefined {
7-
return workspace.getConfiguration(this.SECTION).get<T>(key);
6+
public get<T = string>(key: string, defaultValue: T): T {
7+
return workspace.getConfiguration(this.SECTION).get<T>(key, defaultValue);
88
}
99
}
1010

src/common/php/FileHeader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Config from 'common/Config';
22

33
export default class FileHeader {
44
public static getHeader(module: string): string | undefined {
5-
const header = Config.get<string>('phpFileHeaderComment');
5+
const header = Config.get<string>('phpFileHeaderComment', '');
66

77
if (!header) {
88
return undefined;

src/common/xml/FileHeader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Config from 'common/Config';
22

33
export default class FileHeader {
44
public static getHeader(module: string): string | undefined {
5-
const header = Config.get<string>('xmlFileHeaderComment');
5+
const header = Config.get<string>('xmlFileHeaderComment', '');
66

77
if (!header) {
88
return undefined;

src/common/xml/XmlSuggestionProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export abstract class XmlSuggestionProvider<T> {
134134

135135
public canProvideSuggestions(document: TextDocument): boolean {
136136
if (this.getConfigKey()) {
137-
const provideXmlSuggestions = Config.get<boolean>(this.getConfigKey()!);
137+
const provideXmlSuggestions = Config.get<boolean>(this.getConfigKey()!, true);
138138

139139
if (!provideXmlSuggestions) {
140140
return false;

src/definition/XmlClasslikeDefinitionProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class XmlClasslikeDefinitionProvider implements DefinitionProvider {
2020
position: Position,
2121
token: CancellationToken
2222
) {
23-
const provideXmlDefinitions = Config.get<boolean>('provideXmlDefinitions');
23+
const provideXmlDefinitions = Config.get<boolean>('provideXmlDefinitions', true);
2424

2525
if (!provideXmlDefinitions) {
2626
return null;

src/hover/XmlClasslikeHoverProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Hover, HoverProvider, Position, Range, TextDocument } from 'vscode';
88

99
export default class XmlClasslikeHoverProvider implements HoverProvider {
1010
public async provideHover(document: TextDocument, position: Position): Promise<Hover | null> {
11-
const provideXmlHovers = Config.get<boolean>('provideXmlHovers');
11+
const provideXmlHovers = Config.get<boolean>('provideXmlHovers', true);
1212

1313
if (!provideXmlHovers) {
1414
return null;

src/indexer/IndexManager.ts

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import TemplateIndexer from './template/TemplateIndexer';
2020
import { TemplateIndexData } from './template/TemplateIndexData';
2121
import CronIndexer from './cron/CronIndexer';
2222
import { CronIndexData } from './cron/CronIndexData';
23+
import Config from 'common/Config';
2324

2425
type IndexerInstance =
2526
| DiIndexer
@@ -41,7 +42,7 @@ type IndexerDataMap = {
4142
};
4243

4344
class IndexManager {
44-
private static readonly INDEX_BATCH_SIZE = 50;
45+
private static readonly REPORTING_BATCH_SIZE = 500;
4546

4647
protected indexers: IndexerInstance[] = [];
4748
protected indexStorage: IndexStorage;
@@ -77,40 +78,60 @@ class IndexManager {
7778
Logger.logWithTime('Indexing workspace', workspaceFolder.name);
7879

7980
for (const indexer of this.indexers) {
80-
this.indexStorage.loadIndex(workspaceFolder, indexer.getId(), indexer.getVersion());
81-
82-
if (!force && !this.shouldIndex(workspaceFolder, indexer)) {
83-
Logger.logWithTime('Loaded index from storage', workspaceFolder.name, indexer.getId());
84-
continue;
81+
if (!force) {
82+
Logger.logWithTime('Loading index from storage', workspaceFolder.name, indexer.getId());
83+
this.indexStorage.loadIndex(workspaceFolder, indexer.getId(), indexer.getVersion());
84+
85+
if (!this.shouldIndex(workspaceFolder, indexer)) {
86+
Logger.logWithTime('Loaded index from storage', workspaceFolder.name, indexer.getId());
87+
continue;
88+
}
8589
}
86-
progress.report({ message: `Indexing - ${indexer.getName()}`, increment: 0 });
90+
Logger.logWithTime('Indexing', indexer.getName());
8791

88-
const indexData = this.getIndexStorageData(indexer.getId()) || new Map();
92+
progress.report({
93+
message: `Indexing - ${indexer.getName()} [...]`,
94+
});
8995

90-
Logger.logWithTime('Indexing', indexer.getName());
91-
const files = await workspace.findFiles(indexer.getPattern(workspaceUri), 'dev/**');
96+
const indexData = new Map();
97+
98+
Logger.logWithTime('Discovering files to index');
99+
100+
const files = await workspace.findFiles(
101+
indexer.getPattern(workspaceUri),
102+
indexer.getExcludePattern(workspaceUri)
103+
);
92104

93105
let doneCount = 0;
94106
const totalCount = files.length;
107+
Logger.logWithTime('Found', totalCount, 'files to index');
95108

96-
for (let i = 0; i < files.length; i += IndexManager.INDEX_BATCH_SIZE) {
97-
const batch = files.slice(i, i + IndexManager.INDEX_BATCH_SIZE);
109+
const batchSize = Config.get<number>('magento-toolbox.indexingBatchSize', 25);
110+
111+
for (let i = 0; i < files.length; i += batchSize) {
112+
const batch = files.slice(i, i + batchSize);
98113

99114
await Promise.all(
100115
batch.map(async file => {
101-
const data = await indexer.indexFile(file);
116+
try {
117+
const data = await indexer.indexFile(file);
102118

103-
if (data !== undefined) {
104-
indexData.set(file.fsPath, data);
105-
}
119+
if (data !== undefined) {
120+
indexData.set(file.fsPath, data);
121+
}
106122

107-
doneCount++;
108-
const pct = Math.round((doneCount / totalCount) * 100);
123+
doneCount++;
109124

110-
progress.report({
111-
message: `Indexing - ${indexer.getName()} [${doneCount}/${totalCount}]`,
112-
increment: pct,
113-
});
125+
if (doneCount % IndexManager.REPORTING_BATCH_SIZE === 0) {
126+
Logger.logWithTime('Indexed', doneCount, 'files of', totalCount);
127+
128+
progress.report({
129+
message: `Indexing - ${indexer.getName()} [${doneCount}/${totalCount}]`,
130+
});
131+
}
132+
} catch (error) {
133+
Logger.error('Error indexing file', file.fsPath, String(error));
134+
}
114135
})
115136
);
116137
}
@@ -122,7 +143,7 @@ class IndexManager {
122143

123144
Logger.logWithTime('Indexing', indexer.getName(), 'done');
124145

125-
progress.report({ increment: 100 });
146+
progress.report({ message: `Indexing - ${indexer.getName()} [done]` });
126147
}
127148

128149
Logger.logWithTime('Finished indexing workspace', workspaceFolder.name);

src/indexer/Indexer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { type GlobPattern, type Uri } from 'vscode';
1+
import { type GlobPattern, type Uri, RelativePattern } from 'vscode';
22
import { IndexerKey } from 'types/indexer';
33

44
export abstract class Indexer<D = any> {
55
public abstract getId(): IndexerKey;
66
public abstract getName(): string;
77
public abstract getPattern(uri: Uri): GlobPattern;
8+
9+
public getExcludePattern(uri: Uri): GlobPattern | null {
10+
return new RelativePattern(uri, '{**/node_modules/**,**/dev/**,**/pub/**}');
11+
}
12+
813
public abstract indexFile(uri: Uri): Promise<D | undefined>;
914
public abstract getVersion(): number;
1015
}

src/util/Logger.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import { window } from 'vscode';
33
export class Logger {
44
private channel = window.createOutputChannel('Magento Toolbox');
55

6-
public log(...message: string[]) {
6+
public log(...message: (string | number)[]) {
77
this.channel.appendLine(message.join(' '));
8+
console.log('[Magento Toolbox]', ...message);
89
}
910

10-
public logWithTime(...message: string[]) {
11+
public logWithTime(...message: (string | number)[]) {
1112
this.log(new Date().toISOString(), ...message);
1213
}
14+
15+
public error(...message: (string | number)[]) {
16+
this.logWithTime('ERROR', ...message);
17+
}
1318
}
1419

1520
export default new Logger();

0 commit comments

Comments
 (0)