@lvce-editor/search-process 15.8.0 → 15.9.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.
Files changed (2) hide show
  1. package/dist/index.js +31 -6
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1629,6 +1629,30 @@ const getStringIndexFromByteOffset = (text, byteOffset) => {
1629
1629
  return Buffer.from(text).subarray(0, byteOffset).toString().length;
1630
1630
  };
1631
1631
 
1632
+ const ellipsis = '...';
1633
+ const seamSearchRadius = 8;
1634
+ const isNaturalSeam = character => {
1635
+ return /[\s./\\_:;,()[\]{}'"-]/.test(character);
1636
+ };
1637
+ const getPreviewStart = (text, matchStart, charsBefore) => {
1638
+ const preferredStart = Math.max(matchStart - charsBefore, 0);
1639
+ if (preferredStart === 0) {
1640
+ return 0;
1641
+ }
1642
+ const minimumSeamIndex = Math.max(preferredStart - seamSearchRadius, 0);
1643
+ for (let seamIndex = preferredStart - 1; seamIndex >= minimumSeamIndex; seamIndex--) {
1644
+ if (isNaturalSeam(text[seamIndex])) {
1645
+ return seamIndex + 1;
1646
+ }
1647
+ }
1648
+ const maximumSeamIndex = Math.min(preferredStart + seamSearchRadius, matchStart - 1);
1649
+ for (let seamIndex = preferredStart; seamIndex <= maximumSeamIndex; seamIndex++) {
1650
+ if (isNaturalSeam(text[seamIndex])) {
1651
+ return seamIndex + 1;
1652
+ }
1653
+ }
1654
+ return preferredStart;
1655
+ };
1632
1656
  const toTextSearchResult = (parsedLine, remaining, charsBefore, charsAfter) => {
1633
1657
  const results = [];
1634
1658
  const parsedLineData = parsedLine.data;
@@ -1640,15 +1664,16 @@ const toTextSearchResult = (parsedLine, remaining, charsBefore, charsAfter) => {
1640
1664
  for (const submatch of submatches) {
1641
1665
  const startColumnIndex = getStringIndexFromByteOffset(lines, submatch.start);
1642
1666
  const endColumnIndex = getStringIndexFromByteOffset(lines, submatch.end);
1643
- const previewStart = Math.max(startColumnIndex - charsBefore, 0);
1667
+ const previewStart = getPreviewStart(lines, startColumnIndex, charsBefore);
1644
1668
  const previewEnd = Math.min(endColumnIndex + charsAfter, lines.length);
1645
- const previewText = lines.slice(previewStart, previewEnd);
1669
+ const prefix = previewStart === 0 ? '' : ellipsis;
1670
+ const previewText = prefix + lines.slice(previewStart, previewEnd);
1646
1671
  results.push({
1647
- end: endColumnIndex - previewStart,
1672
+ end: endColumnIndex - previewStart + prefix.length,
1648
1673
  endColumnIndex,
1649
1674
  lineNumber,
1650
1675
  rowIndex: lineNumber - 1,
1651
- start: startColumnIndex - previewStart,
1676
+ start: startColumnIndex - previewStart + prefix.length,
1652
1677
  startColumnIndex,
1653
1678
  text: previewText,
1654
1679
  type: Match
@@ -1912,7 +1937,7 @@ const textSearchIncremental = async ({
1912
1937
  const charsBefore = 26;
1913
1938
  const charsAfter = 50;
1914
1939
  const childProcess = spawn(ripGrepArgs, {
1915
- cwd: searchDir
1940
+ cwd: ensurePath(searchDir)
1916
1941
  });
1917
1942
  const pipeLinePromise = collectStdoutIncremental(id, childProcess.stdout, childProcess.kill, maxSearchResults, charsBefore, charsAfter);
1918
1943
  const closePromise = waitForProcessToExit(childProcess);
@@ -2035,7 +2060,7 @@ const textSearchPull = async (rpc, {
2035
2060
  uid
2036
2061
  }) => {
2037
2062
  const childProcess = spawn(ripGrepArgs, {
2038
- cwd: searchDir
2063
+ cwd: ensurePath(searchDir)
2039
2064
  });
2040
2065
  const notifyResultsFound = () => {
2041
2066
  // @ts-ignore
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/search-process",
3
- "version": "15.8.0",
3
+ "version": "15.9.0",
4
4
  "description": "Search Process",
5
5
  "keywords": [
6
6
  "Lvce Editor",