Skip to content

Commit

Permalink
wip: cleaning path up in each handler which works with filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
alashchev17 committed Oct 18, 2024
1 parent 8668709 commit 9ce65a2
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions src/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,18 +601,19 @@ export class PanelWebview implements vscode.WebviewViewProvider {
// }

async deleteFile(fileName: string) {
const pathToFile = vscode.Uri.file(fileName);
const uri = this.filePathToUri(fileName);
const edit = new vscode.WorkspaceEdit();
edit.deleteFile(pathToFile);
edit.deleteFile(uri);
return vscode.workspace.applyEdit(edit).then(success => {
if(!success) {
vscode.window.showInformationMessage("Error: could not delete: " + pathToFile);
vscode.window.showInformationMessage("Error: could not delete: " + uri);
}
});
}

createNewFileWithContent(fileName: string, content: string) {
const newFile = vscode.Uri.parse('untitled:' + fileName);
const uri = this.filePathToUri(fileName);
const newFile = vscode.Uri.parse('untitled:' + uri);
vscode.workspace.openTextDocument(newFile).then(document => {
const edit = new vscode.WorkspaceEdit();
edit.insert(newFile, new vscode.Position(0, 0), content);
Expand All @@ -628,7 +629,8 @@ export class PanelWebview implements vscode.WebviewViewProvider {
}

async addDiffToFile(fileName: string, content: string) {
const document = await vscode.workspace.openTextDocument(vscode.Uri.file(fileName));
const uri = this.filePathToUri(fileName);
const document = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(document);

const start = new vscode.Position(0, 0);
Expand All @@ -646,7 +648,9 @@ export class PanelWebview implements vscode.WebviewViewProvider {
}

async editFileWithContent(fileName: string, content: string) {
const document = await vscode.workspace.openTextDocument(vscode.Uri.file(fileName));
const uri = this.filePathToUri(fileName);
const document = await vscode.workspace.openTextDocument(uri);

const start = new vscode.Position(0, 0);
const end = new vscode.Position(document.lineCount, 0);
const range = new vscode.Range(start, end);
Expand Down Expand Up @@ -694,9 +698,20 @@ export class PanelWebview implements vscode.WebviewViewProvider {
return cleanedPath;
}

private filePathToUri(fileName: string) {
// TODO: check if posix paths are sent by the lsp on a windows system
console.log(`[DEBUG]: original file_name: `, fileName);
if (fileName.startsWith('\\?\\')) {
fileName = fileName.slice(3); // Remove the '\\?\' prefix
}
const parsedPath = path.parse(fileName);
return vscode.Uri.file(path.posix.format(parsedPath));
}

async startFileAnimation(fileName: string) {

const openFiles = this.getOpenFiles();
console.log(`[COND]: openFiles: `, openFiles);
const editor = vscode.window.activeTextEditor;

const workspaceFolders = vscode.workspace.workspaceFolders;
Expand All @@ -707,17 +722,23 @@ export class PanelWebview implements vscode.WebviewViewProvider {
const workspace_path = workspaceFolders[0].uri.fsPath;
const relative_path = path.relative(workspace_path, fileName.replace(/\?/g, ''));

const normalized_path = this.removeQuestionMarks(fileName);
const uri = vscode.Uri.file(normalized_path);
const uri = this.filePathToUri(fileName);
console.log(`[COND]: obtained uri: `, {uri: uri});

console.log(`[COND]: openFiles.some(file => file.includes(relative_path): ${openFiles.some(file => file.includes(relative_path))}`);
if(!openFiles.some(file => file.includes(relative_path))|| !editor) {return;}
const document = await vscode.workspace.openTextDocument(uri);
let document: vscode.TextDocument | null = null;
if(!openFiles.some(file => file.includes(relative_path))) {
console.log(`[COND]: файла нет, но вы держитесь`);
// document = await vscode.workspace.openTextDocument(uri);
}
if (!editor) { return; }
document = await vscode.workspace.openTextDocument(uri);

const state = estate.state_of_editor(editor, "start_animate for file: " + uri);
if(!state) {return;}
await estate.switch_mode(state, estate.Mode.DiffWait);
const startPosition = new vscode.Position(0, 0);
if (!document) {return;}
const endPosition = new vscode.Position(document.lineCount - 1, document.lineAt(document.lineCount - 1).text.length);
state.showing_diff_for_range = new vscode.Range(startPosition, endPosition);
animation_start(editor, state);
Expand All @@ -728,8 +749,8 @@ export class PanelWebview implements vscode.WebviewViewProvider {
const openFiles = this.getOpenFiles();
const editor = vscode.window.activeTextEditor;

const normalized_path = this.removeQuestionMarks(fileName);
const uri = vscode.Uri.file(normalized_path);
const uri = this.filePathToUri(fileName);
console.log(`[DEBUG]: obtained uri: `, {uri: uri});

const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
Expand Down Expand Up @@ -837,9 +858,11 @@ export class PanelWebview implements vscode.WebviewViewProvider {


async handleOpenFile(file: {file_name:string, line?: number}) {
const normalized_path = this.removeQuestionMarks(file.file_name);
// const normalized_path = this.removeQuestionMarks(file.file_name);

const uri = vscode.Uri.file(normalized_path);
// const uri = vscode.Uri.file(normalized_path);
const uri = this.filePathToUri(file.file_name);
console.log(`[DEBUG]: obtained uri: `, {uri: uri});
const document = await vscode.workspace.openTextDocument(uri);

if(file.line !== undefined) {
Expand Down

0 comments on commit 9ce65a2

Please sign in to comment.