Skip to content

Commit

Permalink
TMP logging to work out why explorer view is now broken! (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
HughG committed Jun 4, 2023
1 parent 3f0db87 commit 51e9c74
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function activate(context: ExtensionContext) {

// TreeView of Sphinx-Needs Objects
const needsExplorerProvider = new NeedsExplorerProvider();
console.info('SNV: Created NeedsExplorerProvider.');
window.createTreeView('sphinxNeedsExplorer', {
treeDataProvider: needsExplorerProvider
});
Expand Down
33 changes: 32 additions & 1 deletion client/src/needsExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
// Initial logger
tslogger = new TimeStampedLogger(this.snvConfigs.loggingLevel);

tslogger.info("SNV Explorer -> constructor");

// Check needsJson and srcDir from workspace configurations
this.check_wk_configs();
tslogger.info("SNV Explorer -> checked configs");

// Load all needsJsons from workspace configurations
this.needsInfos = this.loadAllNeedsJsonsToInfos();
Expand Down Expand Up @@ -129,13 +132,15 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
const watcher = vscode.workspace.createFileSystemWatcher('**/*.json');
// Watch for file content change
watcher.onDidChange((uri) => {
tslogger.info(`SVN Explorer -> File change: ${uri}`)
if (all_curr_needs_jsons.indexOf(uri.fsPath) >= 0) {
this.needsInfos[uri.fsPath] = this.loadNeedsJsonToInfo(uri.fsPath);
this._onDidChangeTreeData.fire(undefined);
}
});
// Watch for file create
watcher.onDidCreate((uri) => {
tslogger.info(`SVN Explorer -> File creation: ${uri}`)
if (all_curr_needs_jsons.indexOf(uri.fsPath) >= 0) {
this.needsInfos[uri.fsPath] = this.loadNeedsJsonToInfo(uri.fsPath);
this._onDidChangeTreeData.fire(undefined);
Expand All @@ -157,6 +162,7 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre

private listenToChangeConfiguration(): void {
vscode.workspace.onDidChangeConfiguration(() => {
tslogger.info(`SVN Explorer -> Configuration change`)
let updateTreeData = false;
const newConfig = this.getSNVConfigurations();

Expand Down Expand Up @@ -229,24 +235,29 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
}

getChildren(element?: NeedTree | NeedOptionItem): Thenable<vscode.TreeItem[]> {
tslogger.info(`SNV NeedsExplorerProvider -> getChildren(${element?.id} = ${element?.label})`);
if (!this.needsInfo.needs) {
tslogger.info(`SNV NeedsExplorerProvider -> getChildren: No needs!`);
return Promise.resolve([]);
}

if (!element) {
// Root level
tslogger.info(`SNV NeedsExplorerProvider -> getChildren: root`);
return Promise.resolve(this.getNeedTree());
}

// Get and show need options
if (element.id && element.id in this.needsInfo.needs && this.snvConfigs.explorerOptions) {
const optionItems: NeedOptionItem[] = [];
this.snvConfigs.explorerOptions.forEach((option) => {
tslogger.info(`SNV NeedsExplorerProvider -> getChildren: option ${option} for ${element.id}`);
if (element.id) {
// check if option exists in needs.json
if (option in this.needsInfo.needs[element.id]) {
for (const [need_option, op_value] of Object.entries(this.needsInfo.needs[element.id])) {
if (option === need_option) {
tslogger.info(`SNV NeedsExplorerProvider -> getChildren: option ${option} for ${element.id} == ${op_value}`);
optionItems.push(
new NeedOptionItem(option + ': ' + op_value, vscode.TreeItemCollapsibleState.None)
);
Expand All @@ -264,6 +275,7 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
}

getTreeItem(element: NeedTree): vscode.TreeItem | Thenable<vscode.TreeItem> {
tslogger.info(`SNV NeedsExplorerProvider -> getTreeItem(${element}))`);
return element;
}

Expand All @@ -288,8 +300,14 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0
? vscode.workspace.workspaceFolders[0].uri.fsPath
: undefined;
console.info(`SNV Explorer -> vscode.workspace.workspaceFolders: ${vscode.workspace.workspaceFolders}`);
const conf_folders: DocConf[] = [];
if (workspaceFolderpath) {
if (vscode.workspace.workspaceFolders) {
let folder0 = vscode.workspace.workspaceFolders[0]
console.info(`SNV Explorer -> vscode.workspace.workspaceFolders[0]: ${folder0}`);
console.info(`SNV Explorer -> vscode.workspace.workspaceFolders[0].uri: ${folder0.uri}`);
}
needs_json_path = needs_json_path?.replace('${workspaceFolder}', workspaceFolderpath);
confPyDir = confPyDir?.replace('${workspaceFolder}', workspaceFolderpath);
wk_folders?.forEach((folder) => {
Expand All @@ -298,6 +316,7 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
srcDir: folder.srcDir.replace('${workspaceFolder}', workspaceFolderpath)
});
});
console.info(`SNV Explorer -> replaced workspaceFolder path: ${workspaceFolderpath}`);
} else {
console.error(`SNV Explorer -> Can't resolve current workspaceFolder path: ${workspaceFolderpath}`);
}
Expand Down Expand Up @@ -351,6 +370,7 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
}

private loadAllNeedsJsonsToInfos(): NeedsInfos {
tslogger.info('SNV Explorer -> Loading needs JSON');
const all_needs_infos: NeedsInfos = {};
// Load needsJson from sphinx-needs.folders
this.snvConfigs.folders.forEach((fd) => {
Expand All @@ -362,18 +382,21 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
});
// Load sphinx-needs.needsJson
if (this.snvConfigs.needsJson && this.snvConfigs.srcDir && !(this.snvConfigs.needsJson in all_needs_infos)) {
tslogger.info(`SNV Explorer -> Loading sphinx-needs.needsJson from ${this.snvConfigs.needsJson}`);
all_needs_infos[this.snvConfigs.needsJson] = this.loadNeedsJsonToInfo(this.snvConfigs.needsJson);
}
return all_needs_infos;
}

private loadNeedsJsonToInfo(needsJsonFilePath: string | undefined): NeedsInfo | undefined {
// Check needs.json path and get needs object from needs.json if exists
tslogger.info(`SNV Explorer -> loadNeedsJsonToInfo: ${needsJsonFilePath}`);
if (needsJsonFilePath && this.pathExists(needsJsonFilePath)) {
tslogger.debug(`SNV Explorer -> Loaded nedds json: ${needsJsonFilePath}`);
tslogger.debug(`SNV Explorer -> Loading needs json: ${needsJsonFilePath}`);

// Read needs.json
const needsJson = JSON.parse(fs.readFileSync(needsJsonFilePath, 'utf-8'));
tslogger.debug(`SNV Explorer -> Loaded needs json: ${JSON.stringify(needsJson)}`);

// Get needs objects from current_version
const curr_version: string = needsJson['current_version'];
Expand Down Expand Up @@ -455,18 +478,22 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
let curr_need_file_path = '';
if (this.needsInfo.src_dir) {
curr_need_file_path = path.resolve(this.needsInfo.src_dir, curr_need.docname + curr_need.doctype);
tslogger.warn(`SNV Explorer -> doc path for Need ${need.id} is ${curr_need_file_path}`);
if (!this.pathExists(curr_need_file_path)) {
tslogger.warn(`SNV Explorer -> doc path for Need ${need.id} not exists: ${curr_need_file_path}`);
}
}
const needFileUri: vscode.Uri = vscode.Uri.file(curr_need_file_path);
tslogger.warn(`SNV Explorer -> neddFileUri for Need ${need.id} is ${needFileUri}`);
return needFileUri;
}

private getNeedTree(): NeedTree[] {
const needsItems: NeedTree[] = [];
tslogger.info(`SNV NeedsExplorerProvider -> getNeedTree`);
if (this.needsInfo.needs) {
Object.values(this.needsInfo.needs).forEach((need) => {
tslogger.info(`SNV NeedsExplorerProvider -> getNeedTree: processing ${need.id}`);
// Check if Need ID matches Need Objects key entry
if (!(need['id'] in this.needsInfo.needs)) {
tslogger.warn(`SNV Explorer -> Need object entry of ${need.id} not exits in given needs.json`);
Expand All @@ -486,6 +513,7 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
});
// Get need ID Definition Location
const needFileUri = this.getNeedFilePath(need);
tslogger.info(`SNV NeedsExplorerProvider -> getNeedTree: needFileUri for ${need.id} = ${needFileUri}`);
let needIDPos;
try {
needIDPos = findDefinition(need, needFileUri);
Expand All @@ -510,6 +538,7 @@ export class NeedsExplorerProvider implements vscode.TreeDataProvider<vscode.Tre
});
return needsItems;
} else {
tslogger.info(`SNV NeedsExplorerProvider -> getNeedTree: no needs!`);
return [];
}
}
Expand Down Expand Up @@ -563,12 +592,14 @@ function findDefinition(need: Need, fileUri: vscode.Uri): vscode.Range | undefin
// Read the document where Need ID is at
const doc_contents = read_need_doc_contents(fileUri);
if (!doc_contents) {
tslogger.info(`SNV NeedsExplorerProvider -> findDefinition: failed to read ${fileUri} for ${need.id}`);
return;
}

// Get location of need directive definition line index, e.g. .. req::
const need_directive_location = find_directive_definition(doc_contents, need);
if (!need_directive_location) {
tslogger.info(`SNV NeedsExplorerProvider -> findDefinition: failed to find directive in ${fileUri} for ${need.id}`);
return;
}

Expand Down
3 changes: 3 additions & 0 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,17 @@ connection.onDidChangeWatchedFiles((_change) => {
function read_needs_json(given_needs_json_path: string) {
// Check if given needs.json path exists
const needs_json_path = given_needs_json_path;
tslogger.info(`SNV: Reading needs from ${needs_json_path}`);

if (!fs.existsSync(needs_json_path)) {
tslogger.info(`SNV: Needs file does not exist: ${needs_json_path}`);
return;
}

try {
const data = fs.readFileSync(needs_json_path, 'utf8');
const needs_json: NeedsJsonObj = JSON.parse(data);
tslogger.debug(`SNV: Read needs ${JSON.stringify(needs_json)}`);
return needs_json;
} catch (err) {
tslogger.error(`SNV: Error reading NeedsJson: ${err}`);
Expand Down

0 comments on commit 51e9c74

Please sign in to comment.