Skip to content

Commit

Permalink
wip(bookmark): 修复Refactor指令中的Move to new file 书签异常问题
Browse files Browse the repository at this point in the history
  • Loading branch information
czfadmin committed Dec 6, 2024
1 parent f387388 commit 3940d4f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 53 deletions.
85 changes: 32 additions & 53 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {registerExtensionCustomContextByKey} from './context';
import {resolveBookmarkController} from './bootstrap';
import resolveServiceManager, {ServiceManager} from './services/ServiceManager';
import {Locker} from './stores/EventLocker';

let onDidChangeVisibleTextEditors: Disposable | undefined;
let onDidSaveTextDocumentDisposable: Disposable | undefined;
Expand All @@ -26,15 +27,9 @@ let onDidRenameFilesDisposable: Disposable | undefined;
let onDidDeleteFilesDisposable: Disposable | undefined;
let onDidTextSelectionDisposable: Disposable | undefined;

export const locker = {
refactoring: false,
didCreate: false,
willCreate: false,
didDelete: false,
willDelete: false,
didRename: false,
willRename: false,
};
export const locker = Locker.create({
didCreate: [],
});

export function updateChangeActiveTextEditorListener() {
const sm = resolveServiceManager();
Expand Down Expand Up @@ -159,22 +154,33 @@ export function updateBookmarkInfoWhenTextChangeListener() {
return;
}

// 配合当使用`command(refactor:moveToNewFile)`指令, 更新书签数据
if (bookmarks.length === 1 && locker.didCreate) {
locker.didCreate = false;
const bookmark = bookmarks[0];
if (
locker.didCreate.filter(it => it.fileId === document.uri.fsPath).length &&
locker.didCreate.length
) {
// 配合当使用`command(refactor:moveToNewFile)`指令, 更新书签数据
locker.didCreate
.filter(it => it.fileId === document.uri.fsPath)
.forEach(it => {
for (let bookmark of bookmarks) {
// 寻找第一个相同的内容的书签作为书签的新的 `range`
for (let idx = 0; idx < document.lineCount; idx++) {
const line = document.lineAt(idx);
if (
line.text.length &&
bookmark.selectionContent
.replace(/\s+/g, '')
.includes(line.text.trim().replace(/\s+/g, ''))
) {
bookmark.updateRangesOrOptions(line.range);
break;
}
}
}

locker.removeDidCreateByFileId(it.fileId);
});

// 寻找第一个相同的内容的书签作为书签的新的 `range`
for (let idx = 0; idx < document.lineCount; idx++) {
const line = document.lineAt(idx);
if (
line.text.length &&
bookmark.selectionContent.includes(line.text.trim())
) {
bookmark.updateRangesOrOptions(line.range);
}
return;
}
return;
}

Expand Down Expand Up @@ -209,8 +215,6 @@ export function updateFilesRenameAndDeleteListeners() {
}
});

workspace.onWillRenameFiles(e => {});

// 监听文件删除
onDidDeleteFilesDisposable = workspace.onDidDeleteFiles(e => {
const {files} = e;
Expand All @@ -234,7 +238,6 @@ export function updateFilesRenameAndDeleteListeners() {
* - 如果没用, 更新当前激活的文件中的存在的书签的数据, 此时已经有`updateBookmarkInfoWhenTextChangeListener` 监听的事件进行处理.
*/
workspace.onDidCreateFiles((e: FileCreateEvent) => {
locker.willCreate = false;
const {files} = e;
console.log(
'workspace.onDidCreateFiles:',
Expand All @@ -256,33 +259,9 @@ export function updateFilesRenameAndDeleteListeners() {
}

existingBookmark.updateFileUri(files[0]);
locker.didCreate = true;
});

workspace.onWillCreateFiles((e: FileWillCreateEvent) => {
const {files} = e;
console.log(
'workspace.onWillCreateFiles:',
files,
window.activeTextEditor?.selection.active.line,
);
locker.willCreate = true;
});

workspace.onWillSaveTextDocument(e => {
console.log(
'workspace.onWillSaveTextDocument:',
e,
window.activeTextEditor?.selection.active.line,
);
});

workspace.onDidSaveTextDocument(e => {
console.log(
'workspace.onDidSaveTextDocument:',
e,
window.activeTextEditor?.selection.active.line,
);
// 添加到正在创建的书签列表中
locker.updateDidCreate(files[0].fsPath, [existingBookmark.id]);
});
}

Expand Down
37 changes: 37 additions & 0 deletions src/stores/EventLocker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {types} from 'mobx-state-tree';

export const LockerItem = types.model('LockerItem', {
bookmarks: types.array(types.string),
fileId: types.string,
});

export const Locker = types
.model('locker', {
didCreate: types.array(LockerItem),
})
.actions(self => {
return {
updateDidCreate(fileId: string, bookmarks: string[]) {
const existing = self.didCreate.find(it => it.fileId === fileId);
if (!existing) {
self.didCreate.push({
fileId,
bookmarks,
});
} else {
bookmarks.forEach(it => {
if (!existing.bookmarks.find(item => item === it)) {
existing.bookmarks.push(it);
}
});
}
},
removeDidCreateByFileId(fileId: string) {
const existing = self.didCreate.find(it => it.fileId === fileId);
if (!existing) {
return;
}
self.didCreate.remove(existing);
},
};
});

0 comments on commit 3940d4f

Please sign in to comment.