Skip to content

Commit 147275e

Browse files
committed
allow 'non-existed' source folders
1 parent 671734f commit 147275e

4 files changed

Lines changed: 87 additions & 66 deletions

File tree

lib/node-utility

src/EIDEProject.ts

Lines changed: 79 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export interface FolderInfo {
9696
}
9797

9898
interface SourceRootInfo extends FolderInfo {
99+
isValid: () => boolean;
99100
fileWatcher: FileWatcher;
100101
fileGroups: ProjectFileGroup[];
101102
incList: string[];
@@ -400,13 +401,7 @@ class SourceRootList implements SourceProvider {
400401
const srcFolders = this.project.GetConfiguration().config.srcDirs;
401402
srcFolders.forEach((path) => {
402403
const f = new File(path);
403-
if (f.IsDir()) {
404-
const key: string = this.getRelativePath(path);
405-
const watcher = platform.createSafetyFileWatcher(f, true).Watch();
406-
watcher.on('error', (err) => GlobalEvent.emit('msg', ExceptionToMessage(err, 'Hidden')));
407-
watcher.OnRename = (file) => this.onFolderRenamed(key, file);
408-
this.srcFolderMaps.set(key, this.newSourceInfo(key, watcher));
409-
}
404+
this._add(f);
410405
});
411406

412407
// update all
@@ -417,19 +412,21 @@ class SourceRootList implements SourceProvider {
417412
if (!notEmitEvt) { this.emit('dataChanged', 'dataChanged'); }
418413
}
419414

415+
private _add(dir: File): SourceRootInfo {
416+
const key: string = this.getRelativePath(dir.path);
417+
const watcher = platform.createSafetyFileWatcher(dir, true);
418+
watcher.on('error', (err) => GlobalEvent.emit('globalLog', ExceptionToMessage(err, 'Warning')));
419+
watcher.OnRename = (file) => this.onFolderRenamed(key, file);
420+
const sourceInfo = this.newSourceInfo(key, watcher);
421+
this.srcFolderMaps.set(key, sourceInfo);
422+
return sourceInfo;
423+
}
424+
420425
add(absPath: string): boolean {
421-
const f = new File(absPath);
422-
if (f.IsDir()) {
423-
const key: string = this.getRelativePath(absPath);
424-
const watcher = platform.createSafetyFileWatcher(f, true).Watch();
425-
watcher.on('error', (err) => GlobalEvent.emit('msg', ExceptionToMessage(err, 'Hidden')));
426-
watcher.OnRename = (file) => this.onFolderRenamed(key, file);
427-
const sourceInfo = this.newSourceInfo(key, watcher);
428-
this.srcFolderMaps.set(key, sourceInfo);
429-
this.updateFolder(sourceInfo);
430-
return true;
431-
}
432-
return false;
426+
const dir = new File(absPath);
427+
const sourceInfo = this._add(dir);
428+
this.updateFolder(sourceInfo);
429+
return true;
433430
}
434431

435432
remove(absPath: string): boolean {
@@ -470,7 +467,7 @@ class SourceRootList implements SourceProvider {
470467
if (rootSrcUpdateList.length > 0) {
471468

472469
for (const rootInfo of rootSrcUpdateList) {
473-
if (rootInfo.fileWatcher.file.IsDir()) {
470+
if (rootInfo.isValid()) {
474471
this.updateFolder(rootInfo, [targetDir]);
475472
}
476473
}
@@ -527,6 +524,26 @@ class SourceRootList implements SourceProvider {
527524
return res;
528525
}
529526

527+
isIncludes(abspath: string): boolean {
528+
529+
if (!File.isAbsolute(abspath)) {
530+
abspath = this.project.toAbsolutePath(abspath);
531+
}
532+
533+
const unixPath = File.ToUnixPath(abspath);
534+
535+
for (const rootInfo of this.srcFolderMaps.values()) {
536+
const unixRootPath = File.ToUnixPath(rootInfo.fileWatcher.file.path);
537+
const unixRootRealPath = File.ToUnixPath(platform.realpathSync(rootInfo.fileWatcher.file.path));
538+
if (unixPath.startsWith(unixRootPath) ||
539+
unixPath.startsWith(unixRootRealPath)) {
540+
return true;
541+
}
542+
}
543+
544+
return false;
545+
}
546+
530547
forceUpdateAllFolders() {
531548
for (const info of this.srcFolderMaps.values()) {
532549
this.updateFolder(info);
@@ -537,13 +554,8 @@ class SourceRootList implements SourceProvider {
537554
forceUpdateFolder(rePath: string) {
538555
const rootInfo = this.srcFolderMaps.get(rePath);
539556
if (rootInfo) {
540-
if (rootInfo.fileWatcher.file.IsDir()) {
541-
this.updateFolder(rootInfo);
542-
this.emit('dataChanged', 'folderChanged');
543-
} else {
544-
this.removeByKey(rePath);
545-
this.emit('dataChanged', 'dataChanged');
546-
}
557+
this.updateFolder(rootInfo);
558+
this.emit('dataChanged', 'folderChanged');
547559
}
548560
}
549561

@@ -562,34 +574,41 @@ class SourceRootList implements SourceProvider {
562574
return {
563575
displayName: displayName,
564576
fileWatcher: watcher,
577+
isValid: () => watcher.file.IsDir(),
565578
needUpdate: false,
566579
fileGroups: [],
567580
incList: []
568581
};
569582
}
570583

571-
private removeByKey(key: string): boolean {
584+
private disposeWatcher(key: string): void {
572585
this.srcFolderMaps.get(key)?.fileWatcher.Close();
586+
}
587+
588+
private removeByKey(key: string): boolean {
589+
this.disposeWatcher(key);
573590
return this.srcFolderMaps.delete(key);
574591
}
575592

576593
private onFolderRenamed(folderKey: string, targetFile: File) {
577594
const rootInfo = this.srcFolderMaps.get(folderKey);
578595
if (rootInfo) {
579-
if (targetFile.path === rootInfo.fileWatcher.file.path) { // root folder has been renamed
580-
if (this.removeByKey(folderKey)) { this.emit('dataChanged', 'dataChanged'); }
596+
597+
if (targetFile.path == rootInfo.fileWatcher.file.path) { // root folder has been renamed
598+
this.disposeWatcher(folderKey);
599+
this.emit('dataChanged', 'folderStatusChanged');
600+
}
601+
602+
if (rootInfo.refreshTimeout) {
603+
rootInfo.refreshTimeout.refresh();
581604
} else {
582-
if (rootInfo.refreshTimeout) {
583-
rootInfo.refreshTimeout.refresh();
584-
} else {
585-
rootInfo.refreshTimeout = setTimeout((folderInfo: SourceRootInfo) => {
586-
if (folderInfo.refreshTimeout) {
587-
folderInfo.refreshTimeout = undefined;
588-
this.updateFolder(folderInfo);
589-
this.emit('dataChanged', 'folderChanged');
590-
}
591-
}, 100, rootInfo);
592-
}
605+
rootInfo.refreshTimeout = setTimeout((folderInfo: SourceRootInfo) => {
606+
if (folderInfo.refreshTimeout) {
607+
folderInfo.refreshTimeout = undefined;
608+
this.updateFolder(folderInfo);
609+
this.emit('dataChanged', 'folderChanged');
610+
}
611+
}, 200, rootInfo);
593612
}
594613
}
595614
}
@@ -619,6 +638,9 @@ class SourceRootList implements SourceProvider {
619638
rootFolderInfo.incList = [];
620639
rootFolderInfo.fileGroups = [];
621640
folderStack.push(rootFolder);
641+
// if source root have no watcher, watch it !
642+
if (rootFolderInfo.isValid() && !rootFolderInfo.fileWatcher.IsWatched())
643+
rootFolderInfo.fileWatcher.Watch();
622644
}
623645

624646
rootFolderInfo.needUpdate = false;
@@ -629,9 +651,14 @@ class SourceRootList implements SourceProvider {
629651

630652
const cFolder = <File>folderStack.pop();
631653
const isSourceRoot = cFolder.path === rootFolder.path;
632-
if (cFolder.name.startsWith('.') && !isSourceRoot) continue; // skip '.xxx' folders, not root folder
633-
const fileList = cFolder.GetList(fileFilter, File.EXCLUDE_ALL_FILTER);
634654

655+
if (!cFolder.IsDir())
656+
continue; // skip not-existed folder
657+
658+
if (cFolder.name.startsWith('.') && !isSourceRoot)
659+
continue; // skip sub '.xxx' folders, but not root folder
660+
661+
const fileList = cFolder.GetList(fileFilter, File.EXCLUDE_ALL_FILTER);
635662
if (fileList.length > 0) {
636663

637664
// filter source file and add to file group
@@ -679,7 +706,7 @@ class SourceRootList implements SourceProvider {
679706

680707
} catch (error) {
681708
rootFolderInfo.needUpdate = true; // set need update flag
682-
GlobalEvent.emit('msg', ExceptionToMessage(error, 'Hidden'));
709+
GlobalEvent.emit('globalLog', ExceptionToMessage(error, 'Warning'));
683710
}
684711
}
685712
}
@@ -724,7 +751,7 @@ export abstract class AbstractProject implements CustomConfigurationProvider, Pr
724751
static readonly excludeDirFilter: RegExp = /^\./;
725752

726753
// to show output files
727-
static readonly buildOutputMatcher: RegExp = /\.(?:elf|axf|out|a|lib|hex|ihx|bin|s19|s37|sct|icf|ld[s]?|map|map\.view)$/i;
754+
static readonly buildOutputMatcher: RegExp = /\.(?:elf|axf|out|a|lib|hex|ihx|bin|s19|s37|sct|icf|ld[s]?|map|map\.view|lst)$/i;
728755

729756
//-------
730757

@@ -1125,7 +1152,7 @@ export abstract class AbstractProject implements CustomConfigurationProvider, Pr
11251152

11261153
replacePathEnv(path: string): string {
11271154

1128-
for (let cnt = 0; cnt < 5; cnt++) {
1155+
for (let cnt = 0; cnt < 3; cnt++) {
11291156

11301157
if (!File.isEnvPath(path))
11311158
break; // not have any env var, end
@@ -2668,6 +2695,7 @@ class EIDEProject extends AbstractProject {
26682695

26692696
const fileAssCfg: any = {
26702697
".eideignore": "ignore",
2698+
"*.a51": "a51",
26712699
"*.h": "c",
26722700
"*.c": "c",
26732701
"*.hxx": "cpp",
@@ -2856,20 +2884,6 @@ class EIDEProject extends AbstractProject {
28562884
//
28572885
}
28582886
}
2859-
2860-
/* const cppConfig = this.GetCppConfig();
2861-
const cppConfigItem = cppConfig.getConfig();
2862-
if (cppConfigItem.configurationProvider) {
2863-
const newCfg: CppConfigItem = {
2864-
name: os.platform(),
2865-
includePath: <any>undefined,
2866-
defines: <any>undefined,
2867-
intelliSenseMode: "${default}",
2868-
configurationProvider: this.extensionId
2869-
};
2870-
cppConfig.setConfig(newCfg);
2871-
cppConfig.saveToFile();
2872-
} */
28732887
}
28742888

28752889
// show warnings if we have
@@ -3110,10 +3124,12 @@ class EIDEProject extends AbstractProject {
31103124
return new Promise((resolve) => {
31113125
const filePath = platform.realpathSync(uri.fsPath);
31123126
const prjRoot = platform.realpathSync(this.GetRootDir().path);
3127+
const allIncPaths = this.cppToolsConfig.includePath;
31133128
resolve(
3114-
AbstractProject.headerFilter.test(filePath) ||
3115-
filePath.startsWith(prjRoot) ||
3116-
this.vSourceList.has(filePath)
3129+
filePath.startsWith(prjRoot) || // All source files in current workspace
3130+
allIncPaths.some(p => filePath.startsWith(p)) || // All .h files in IncludePaths
3131+
this.vSourceList.has(filePath) || // All virtual source files
3132+
this.sourceRoots.isIncludes(filePath) // All source files in linked source folders
31173133
);
31183134
});
31193135
}

src/Platform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ export function osGetNullDev(): string {
141141
return devNull;
142142
}
143143

144-
export function createSafetyFileWatcher(_file: File, _recursive: boolean = false) {
144+
export function createSafetyFileWatcher(_file: File, _recursive: boolean = false, watchSelfDir?: boolean) {
145145
if (osPlatform != 'win32' &&
146146
osPlatform != 'darwin') {
147147
_recursive = false;
148148
}
149-
return new FileWatcher(_file, _recursive);
149+
return new FileWatcher(_file, _recursive, watchSelfDir);
150150
}
151151

152152
export function exeSuffix(): string {

src/StringTable.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ export const view_str$project$needRefresh = [
273273
'Need Refresh'
274274
][langIndex];
275275

276+
export const view_str$project$fileNotExisted = [
277+
'文件不存在',
278+
'File Not Existed'
279+
][langIndex];
280+
276281
export const view_str$project$excludeFolder = [
277282
'已排除的文件夹',
278283
'Excluded Folder'

0 commit comments

Comments
 (0)