@lvce-editor/text-search-worker 2.6.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/textSearchWorkerMain.js +169 -20
- package/package.json +1 -1
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
const commands = Object.create(null);
|
|
2
|
-
const registerCommand = (key, fn) => {
|
|
3
|
-
commands[key] = fn;
|
|
4
|
-
};
|
|
5
2
|
const register = commandMap => {
|
|
6
|
-
|
|
7
|
-
registerCommand(key, value);
|
|
8
|
-
}
|
|
3
|
+
Object.assign(commands, commandMap);
|
|
9
4
|
};
|
|
10
5
|
const getCommand = key => {
|
|
11
6
|
return commands[key];
|
|
@@ -468,7 +463,7 @@ const FocusSearchWholeWord = 34;
|
|
|
468
463
|
const FocusSearchReplaceAll = 35;
|
|
469
464
|
const FocusSearchPreserveCase = 36;
|
|
470
465
|
|
|
471
|
-
const create = (uid, x, y, width, height, workspacePath) => {
|
|
466
|
+
const create = (uid, x, y, width, height, workspacePath, assetDir) => {
|
|
472
467
|
const state = {
|
|
473
468
|
uid,
|
|
474
469
|
searchResults: [],
|
|
@@ -503,7 +498,14 @@ const create = (uid, x, y, width, height, workspacePath) => {
|
|
|
503
498
|
focus: Empty$1,
|
|
504
499
|
loaded: false,
|
|
505
500
|
message: '',
|
|
506
|
-
collapsedPaths: []
|
|
501
|
+
collapsedPaths: [],
|
|
502
|
+
icons: [],
|
|
503
|
+
assetDir,
|
|
504
|
+
deltaY: 0,
|
|
505
|
+
finalDeltaY: 0,
|
|
506
|
+
focused: false,
|
|
507
|
+
focusedIndex: -1,
|
|
508
|
+
handleOffset: 0
|
|
507
509
|
};
|
|
508
510
|
set(uid, state, state);
|
|
509
511
|
};
|
|
@@ -578,6 +580,12 @@ const number = value => {
|
|
|
578
580
|
throw new AssertionError('expected value to be of type number');
|
|
579
581
|
}
|
|
580
582
|
};
|
|
583
|
+
const array = value => {
|
|
584
|
+
const type = getType(value);
|
|
585
|
+
if (type !== 'array') {
|
|
586
|
+
throw new AssertionError('expected value to be of type array');
|
|
587
|
+
}
|
|
588
|
+
};
|
|
581
589
|
const string = value => {
|
|
582
590
|
const type = getType(value);
|
|
583
591
|
if (type !== 'string') {
|
|
@@ -643,6 +651,49 @@ const manyResultsInManyFiles = (resultCount, fileResultCount) => {
|
|
|
643
651
|
PH2: fileResultCount
|
|
644
652
|
});
|
|
645
653
|
};
|
|
654
|
+
const confirmReplaceOneOccurrenceInOneFile = replacement => {
|
|
655
|
+
return i18nString(UiStrings.ConfirmReplaceOneOccurrenceInOneFile, {
|
|
656
|
+
PH1: replacement
|
|
657
|
+
});
|
|
658
|
+
};
|
|
659
|
+
const confirmReplaceOneOccurrenceInOneFileNoValue = () => {
|
|
660
|
+
return i18nString(UiStrings.ConfirmReplaceOneOccurrenceInOneFileNoValue);
|
|
661
|
+
};
|
|
662
|
+
const confirmReplaceManyOccurrencesInOneFile = (matchCount, replacement) => {
|
|
663
|
+
return i18nString(UiStrings.ConfirmReplaceManyOccurrencesInOneFile, {
|
|
664
|
+
PH1: matchCount,
|
|
665
|
+
PH2: replacement
|
|
666
|
+
});
|
|
667
|
+
};
|
|
668
|
+
const confirmReplaceManyOccurrencesInOneFileNoValue = matchCount => {
|
|
669
|
+
return i18nString(UiStrings.ConfirmReplaceManyOccurrencesInOneFileNoValue, {
|
|
670
|
+
PH1: matchCount
|
|
671
|
+
});
|
|
672
|
+
};
|
|
673
|
+
const confirmReplaceManyOccurrencesInManyFiles = (matchCount, fileCount, replacement) => {
|
|
674
|
+
number(matchCount);
|
|
675
|
+
number(fileCount);
|
|
676
|
+
string(replacement);
|
|
677
|
+
return i18nString(UiStrings.ConfirmReplaceManyOccurrencesInManyFiles, {
|
|
678
|
+
PH1: matchCount,
|
|
679
|
+
PH2: fileCount,
|
|
680
|
+
PH3: replacement
|
|
681
|
+
});
|
|
682
|
+
};
|
|
683
|
+
const confirmReplaceManyOccurrencesInManyFilesNoValue = (matchCount, fileCount) => {
|
|
684
|
+
number(matchCount);
|
|
685
|
+
number(fileCount);
|
|
686
|
+
return i18nString(UiStrings.ConfirmReplaceManyOccurrencesInManyFilesNoValue, {
|
|
687
|
+
PH1: matchCount,
|
|
688
|
+
PH2: fileCount
|
|
689
|
+
});
|
|
690
|
+
};
|
|
691
|
+
const replaceAll$1 = () => {
|
|
692
|
+
return i18nString(UiStrings.ReplaceAll);
|
|
693
|
+
};
|
|
694
|
+
const replace = () => {
|
|
695
|
+
return i18nString(UiStrings.Replace);
|
|
696
|
+
};
|
|
646
697
|
const refresh$1 = () => {
|
|
647
698
|
return i18nString(UiStrings.Refresh);
|
|
648
699
|
};
|
|
@@ -961,10 +1012,6 @@ const getActions = () => {
|
|
|
961
1012
|
}];
|
|
962
1013
|
};
|
|
963
1014
|
|
|
964
|
-
const getFileIcon = () => {
|
|
965
|
-
return '';
|
|
966
|
-
};
|
|
967
|
-
|
|
968
1015
|
// TODO this should be in FileSystem module
|
|
969
1016
|
const pathBaseName = path => {
|
|
970
1017
|
return path.slice(path.lastIndexOf('/') + 1);
|
|
@@ -976,7 +1023,7 @@ const getAbsolutePath = relativePath => {
|
|
|
976
1023
|
return `/${relativePath}`; // TODO support windows paths
|
|
977
1024
|
};
|
|
978
1025
|
|
|
979
|
-
const getDisplayResult = (result, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex) => {
|
|
1026
|
+
const getDisplayResult = (result, fileIcons, fileIconIndex, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex) => {
|
|
980
1027
|
const {
|
|
981
1028
|
type,
|
|
982
1029
|
text,
|
|
@@ -995,7 +1042,7 @@ const getDisplayResult = (result, itemHeight, i, setSize, searchTermLength, repl
|
|
|
995
1042
|
title: absolutePath,
|
|
996
1043
|
type: File$1,
|
|
997
1044
|
text: baseName,
|
|
998
|
-
icon:
|
|
1045
|
+
icon: fileIcons[fileIconIndex],
|
|
999
1046
|
posInSet,
|
|
1000
1047
|
setSize,
|
|
1001
1048
|
top,
|
|
@@ -1046,7 +1093,7 @@ const getFilteredResults = (results, collapsedPaths) => {
|
|
|
1046
1093
|
}
|
|
1047
1094
|
return filteredResults;
|
|
1048
1095
|
};
|
|
1049
|
-
const getDisplayResults = (results, itemHeight, resultCount, searchTerm, minLineY, maxLineY, replacement, collapsedPaths, focusedIndex) => {
|
|
1096
|
+
const getDisplayResults = (results, itemHeight, resultCount, searchTerm, minLineY, maxLineY, replacement, collapsedPaths, fileIcons, focusedIndex) => {
|
|
1050
1097
|
const displayResults = [];
|
|
1051
1098
|
const filteredResults = getFilteredResults(results, collapsedPaths);
|
|
1052
1099
|
const setSize = resultCount;
|
|
@@ -1054,12 +1101,14 @@ const getDisplayResults = (results, itemHeight, resultCount, searchTerm, minLine
|
|
|
1054
1101
|
let fileResult = {
|
|
1055
1102
|
matchCount: 0
|
|
1056
1103
|
};
|
|
1104
|
+
let fileIconIndex = 0;
|
|
1057
1105
|
for (let i = minLineY; i < maxLineY; i++) {
|
|
1058
1106
|
const result = filteredResults[i];
|
|
1059
|
-
const displayResult = getDisplayResult(result, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex);
|
|
1107
|
+
const displayResult = getDisplayResult(result, fileIcons, fileIconIndex, itemHeight, i, setSize, searchTermLength, replacement, focusedIndex);
|
|
1060
1108
|
displayResults.push(displayResult);
|
|
1061
1109
|
if (result.type === File$1) {
|
|
1062
1110
|
fileResult = displayResult;
|
|
1111
|
+
fileIconIndex++;
|
|
1063
1112
|
} else {
|
|
1064
1113
|
fileResult.matchCount++;
|
|
1065
1114
|
}
|
|
@@ -1393,6 +1442,21 @@ const handleError = async (error, notify = true, prefix = '') => {
|
|
|
1393
1442
|
console.error(`[text-search-worker] ${error}`);
|
|
1394
1443
|
};
|
|
1395
1444
|
|
|
1445
|
+
const getFileIcons = async files => {
|
|
1446
|
+
// TODO cache file icons
|
|
1447
|
+
const promises = [];
|
|
1448
|
+
for (const file of files) {
|
|
1449
|
+
if (file.type === Match) {
|
|
1450
|
+
continue;
|
|
1451
|
+
}
|
|
1452
|
+
promises.push(invoke$1('IconTheme.getFileIcon', {
|
|
1453
|
+
name: file.text
|
|
1454
|
+
}));
|
|
1455
|
+
}
|
|
1456
|
+
const icons = await Promise.all(promises);
|
|
1457
|
+
return icons;
|
|
1458
|
+
};
|
|
1459
|
+
|
|
1396
1460
|
// TODO optimize this function to return the minimum number
|
|
1397
1461
|
// of visible items needed, e.g. when not scrolled 5 items with
|
|
1398
1462
|
// 20px fill 100px but when scrolled 6 items are needed
|
|
@@ -2145,6 +2209,8 @@ const handleUpdate = async (state, update) => {
|
|
|
2145
2209
|
const numberOfVisible = getNumberOfVisibleItems(listHeight, itemHeight);
|
|
2146
2210
|
const maxLineY = Math.min(numberOfVisible, total);
|
|
2147
2211
|
const finalDeltaY = Math.max(contentHeight - listHeight, 0);
|
|
2212
|
+
const visible = results.slice(0, maxLineY);
|
|
2213
|
+
const icons = await getFileIcons(visible);
|
|
2148
2214
|
return {
|
|
2149
2215
|
...partialNewState,
|
|
2150
2216
|
minLineY: 0,
|
|
@@ -2158,7 +2224,8 @@ const handleUpdate = async (state, update) => {
|
|
|
2158
2224
|
threads,
|
|
2159
2225
|
fileCount,
|
|
2160
2226
|
matchCount: resultCount,
|
|
2161
|
-
loaded: true
|
|
2227
|
+
loaded: true,
|
|
2228
|
+
icons
|
|
2162
2229
|
};
|
|
2163
2230
|
} catch (error) {
|
|
2164
2231
|
handleError(error);
|
|
@@ -2561,7 +2628,7 @@ const renderItems = {
|
|
|
2561
2628
|
return oldState.items === newState.items && oldState.minLineY === newState.minLineY && oldState.maxLineY === newState.maxLineY && oldState.replacement === newState.replacement && oldState.replaceExpanded === newState.replaceExpanded && oldState.matchCase === newState.matchCase && oldState.matchWholeWord === newState.matchWholeWord && oldState.useRegularExpression === newState.useRegularExpression && oldState.message === newState.message && oldState.detailsExpanded === newState.detailsExpanded && oldState.loaded === newState.loaded && oldState.collapsedPaths === newState.collapsedPaths && oldState.listFocusedIndex === newState.listFocusedIndex && oldState.listFocused === newState.listFocused;
|
|
2562
2629
|
},
|
|
2563
2630
|
apply(oldState, newState) {
|
|
2564
|
-
const displayResults = getDisplayResults(newState.items, newState.itemHeight, newState.fileCount, newState.value, newState.minLineY, newState.maxLineY, newState.replacement, newState.collapsedPaths, newState.focusedIndex);
|
|
2631
|
+
const displayResults = getDisplayResults(newState.items, newState.itemHeight, newState.fileCount, newState.value, newState.minLineY, newState.maxLineY, newState.replacement, newState.collapsedPaths, newState.icons, newState.focusedIndex);
|
|
2565
2632
|
const focusOutline = newState.listFocused && newState.listFocusedIndex === -1;
|
|
2566
2633
|
const dom = getSearchVirtualDom(displayResults, newState.replaceExpanded, newState.matchCase, newState.matchWholeWord, newState.useRegularExpression, newState.message, newState.detailsExpanded, focusOutline);
|
|
2567
2634
|
return ['Viewlet.setDom2', newState.uid, dom];
|
|
@@ -2589,7 +2656,7 @@ const renderFocus = {
|
|
|
2589
2656
|
},
|
|
2590
2657
|
apply(newState) {
|
|
2591
2658
|
const selector = getSelector(newState.focus);
|
|
2592
|
-
return ['setFocus', selector];
|
|
2659
|
+
return ['Viewlet.send', newState.uid, 'setFocus', selector];
|
|
2593
2660
|
}
|
|
2594
2661
|
};
|
|
2595
2662
|
const render = [renderItems, renderFocus];
|
|
@@ -2607,6 +2674,86 @@ const doRender = uid => {
|
|
|
2607
2674
|
return commands;
|
|
2608
2675
|
};
|
|
2609
2676
|
|
|
2677
|
+
const getReplaceAllConfirmText = (matchCount, fileCount, replacement) => {
|
|
2678
|
+
if (matchCount === 1) {
|
|
2679
|
+
if (replacement) {
|
|
2680
|
+
return confirmReplaceOneOccurrenceInOneFile(replacement);
|
|
2681
|
+
}
|
|
2682
|
+
return confirmReplaceOneOccurrenceInOneFileNoValue();
|
|
2683
|
+
}
|
|
2684
|
+
if (fileCount === 1) {
|
|
2685
|
+
if (replacement) {
|
|
2686
|
+
return confirmReplaceManyOccurrencesInOneFile(matchCount, replacement);
|
|
2687
|
+
}
|
|
2688
|
+
return confirmReplaceManyOccurrencesInOneFileNoValue(matchCount);
|
|
2689
|
+
}
|
|
2690
|
+
if (replacement) {
|
|
2691
|
+
return confirmReplaceManyOccurrencesInManyFiles(matchCount, fileCount, replacement);
|
|
2692
|
+
}
|
|
2693
|
+
return confirmReplaceManyOccurrencesInManyFilesNoValue(matchCount, fileCount);
|
|
2694
|
+
};
|
|
2695
|
+
|
|
2696
|
+
const replaceAllAndPrompt = async (workspacePath, items, replacement, matchCount, fileCount) => {
|
|
2697
|
+
string(workspacePath);
|
|
2698
|
+
array(items);
|
|
2699
|
+
string(replacement);
|
|
2700
|
+
number(matchCount);
|
|
2701
|
+
number(fileCount);
|
|
2702
|
+
const confirmTitle = replaceAll$1();
|
|
2703
|
+
const confirmAccept = replace();
|
|
2704
|
+
const confirmText = getReplaceAllConfirmText(matchCount, fileCount, replacement);
|
|
2705
|
+
const shouldReplace = await execute('ConfirmPrompt.prompt', confirmText, {
|
|
2706
|
+
title: confirmTitle,
|
|
2707
|
+
confirmMessage: confirmAccept
|
|
2708
|
+
});
|
|
2709
|
+
if (!shouldReplace) {
|
|
2710
|
+
return false;
|
|
2711
|
+
}
|
|
2712
|
+
// TODO
|
|
2713
|
+
// await TextSearchReplaceAll.replaceAll(workspacePath, items, replacement)
|
|
2714
|
+
return true;
|
|
2715
|
+
};
|
|
2716
|
+
|
|
2717
|
+
const getBulkReplacementEdits = (workspacePath, matches) => {
|
|
2718
|
+
string(workspacePath);
|
|
2719
|
+
array(matches);
|
|
2720
|
+
const files = [];
|
|
2721
|
+
const ranges = [];
|
|
2722
|
+
let currentRanges = [];
|
|
2723
|
+
for (const match of matches) {
|
|
2724
|
+
const {
|
|
2725
|
+
type,
|
|
2726
|
+
text
|
|
2727
|
+
} = match;
|
|
2728
|
+
switch (type) {
|
|
2729
|
+
case File$1:
|
|
2730
|
+
ranges.push(currentRanges.length);
|
|
2731
|
+
push(ranges, currentRanges);
|
|
2732
|
+
const absolutePath = `${workspacePath}/${text.slice(2)}`;
|
|
2733
|
+
files.push(absolutePath); // TODO
|
|
2734
|
+
currentRanges = [];
|
|
2735
|
+
break;
|
|
2736
|
+
case Match:
|
|
2737
|
+
currentRanges.push(match.lineNumber - 1, match.matchStart, match.lineNumber - 1, match.matchStart + match.matchLength);
|
|
2738
|
+
break;
|
|
2739
|
+
}
|
|
2740
|
+
}
|
|
2741
|
+
ranges.push(currentRanges.length);
|
|
2742
|
+
push(ranges, currentRanges);
|
|
2743
|
+
return {
|
|
2744
|
+
files,
|
|
2745
|
+
ranges: ranges.slice(1)
|
|
2746
|
+
};
|
|
2747
|
+
};
|
|
2748
|
+
|
|
2749
|
+
const replaceAll = async (workspacePath, items, replacement) => {
|
|
2750
|
+
const {
|
|
2751
|
+
files,
|
|
2752
|
+
ranges
|
|
2753
|
+
} = getBulkReplacementEdits(workspacePath, items);
|
|
2754
|
+
await invoke$1('BulkReplacement.applyBulkReplacement', files, ranges, replacement);
|
|
2755
|
+
};
|
|
2756
|
+
|
|
2610
2757
|
const saveState = uid => {
|
|
2611
2758
|
const {
|
|
2612
2759
|
newState
|
|
@@ -2779,7 +2926,6 @@ const wrapCommand = fn => {
|
|
|
2779
2926
|
const commandMap = {
|
|
2780
2927
|
'TextSearch.clearSearchResults': wrapCommand(clearSearchResults$1),
|
|
2781
2928
|
'TextSearch.copy': wrapCommand(copy),
|
|
2782
|
-
'TextSearch.handleInput': wrapCommand(handleInput),
|
|
2783
2929
|
'TextSearch.dismissItem': wrapCommand(dismissItem),
|
|
2784
2930
|
'TextSearch.focusFirst': wrapCommand(focusFirst),
|
|
2785
2931
|
'TextSearch.focusIndex': wrapCommand(focusIndex),
|
|
@@ -2807,12 +2953,15 @@ const commandMap = {
|
|
|
2807
2953
|
'TextSearch.handleContextMenu': wrapCommand(handleContextMenu),
|
|
2808
2954
|
'TextSearch.handleFocusIn': wrapCommand(handleFocusIn),
|
|
2809
2955
|
'TextSearch.handleIconThemeChange': wrapCommand(handleIconThemeChange),
|
|
2956
|
+
'TextSearch.handleInput': wrapCommand(handleInput),
|
|
2810
2957
|
'TextSearch.handleScrollBarCaptureLost': wrapCommand(handleScrollBarCaptureLost),
|
|
2811
2958
|
'TextSearch.handleScrollBarClick': wrapCommand(handleScrollBarClick),
|
|
2812
2959
|
'TextSearch.handleScrollBarMove': wrapCommand(handleScrollBarMove),
|
|
2813
2960
|
'TextSearch.handleWheel': wrapCommand(handleWheel),
|
|
2814
2961
|
'TextSearch.loadContent': wrapCommand(loadContent),
|
|
2815
2962
|
'TextSearch.refresh': wrapCommand(refresh),
|
|
2963
|
+
'TextSearch.replaceAllAndPrompt': wrapCommand(replaceAllAndPrompt),
|
|
2964
|
+
'TextSearch.replaceAll': wrapCommand(replaceAll),
|
|
2816
2965
|
'TextSearch.submit': wrapCommand(submit),
|
|
2817
2966
|
'TextSearch.toggleReplace': wrapCommand(toggleReplace),
|
|
2818
2967
|
// not wrapped
|