Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "note-manager",
"productName": "Note Manager",
"version": "1.7.0",
"version": "1.7.1",
"description": "A lightweight, fast and multi-functional desktop text editor",
"homepage": "https://github.com/Note-Manager/note-manager#readme",
"repository": {
Expand Down
1 change: 1 addition & 0 deletions src/ipc/SearchHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function registerSearchHandlers() {
searches.set(request.id, worker);
worker.on("message", report);
worker.on("error", error => report(failedProgress(request, summary, error)));
worker.on("exit", code => searches.has(request.id) && report(failedProgress(request, summary, new Error(`the search stopped unexpectedly (exit code ${code})`))));
}));

handle(EventType.CANCEL_FIND_IN_FILES, (event, data) => {
Expand Down
4 changes: 2 additions & 2 deletions src/react/OpenDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {EolType, toEditorText} from "../domain/Encoding";
import {SearchQuery, SearchTarget} from "../domain/Search";
import {FolderSearch, isSearchedFile, parseFileFilters, relativePath} from "../domain/FindInFiles";
import {createSearchDocument, offsetToPosition, replaceMatches, SearchResult, searchText, splitLines} from "../utils/TextSearch";
import {DocumentHits, SearchRun} from "../utils/SearchResults";
import {compactHits, DocumentHits, SearchRun, toCompactDocument} from "../utils/SearchResults";
import {appendReplacement, createReplacementDeltas} from "../utils/UndoHistory";
import {normalizeRows, shiftRows} from "../domain/Bookmarks";
import {refreshIsChanged} from "../utils/TabUtils";
Expand Down Expand Up @@ -36,7 +36,7 @@ export function findInDocuments(source: SearchSource, query: SearchQuery, target
const result = searchTab(source, tab, query);

if (result.error) return {error: result.error};
if (result.matches.length > 0) documents.push({tabId: tab.id || "", name: documentName(source, tab, target), file: tab.file, document: result.document, hits: result.matches});
if (result.matches.length > 0) documents.push({tabId: tab.id || "", name: documentName(source, tab, target), file: tab.file, ...toCompactDocument(compactHits(result.document, result.matches))});
}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/react/components/ToolWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function ToolWindow({tool, context, width, onResize, onClose}: {

shadow.innerHTML = DOMPurify.sanitize(tool.getToolbarWindowContent());

tool.onContentMount?.(shadow, context);
return tool.onContentMount?.(shadow, context);
}, []);

return (
Expand Down
16 changes: 6 additions & 10 deletions src/react/components/editor/plugins/PreviewPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,12 @@ export default class PreviewPlugin extends BasePlugin {
}
};

const unsubscribeAll = () => {
clearTimeout(timer);
unsubscribeContent?.();
unsubscribeLanguage?.();
};

const unsubscribeContent = this.editor?.onChange(() => {
if (!parent.host.isConnected) return unsubscribeAll();

clearTimeout(timer);
timer = setTimeout(render, LIVE_PREVIEW_DELAY);
});

const unsubscribeLanguage = this.editor?.onLanguageChange(() => {
if (!parent.host.isConnected) return unsubscribeAll();

language = this.editor?.getLanguage();
clearTimeout(timer);
render();
Expand All @@ -94,6 +84,12 @@ export default class PreviewPlugin extends BasePlugin {
refreshButton.addEventListener("click", render);

render();

return () => {
clearTimeout(timer);
unsubscribeContent?.();
unsubscribeLanguage?.();
};
},
getToolbarWindowContent: () => {
return `
Expand Down
2 changes: 1 addition & 1 deletion src/react/components/editor/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ export interface ToolWindowContext {
export interface ToolbarMenuItem {
label: string,
icon: string,
onContentMount?: (parent: ShadowRoot, context: ToolWindowContext) => void,
onContentMount?: (parent: ShadowRoot, context: ToolWindowContext) => (() => void) | void,
getToolbarWindowContent: () => string
}
3 changes: 3 additions & 0 deletions src/react/hooks/useEditorSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export function useEditorSearch(editorRef: MutableRefObject<Ace.Editor | undefin
const onEditorLoad = (editor: Ace.Editor) => {
removeOverriddenCommands(editor);
removeScopeMarker.current = undefined;
highlight.current = undefined;
cache.current = undefined;
setScope(undefined);

editor.session.on("change", () => {
Expand All @@ -134,6 +136,7 @@ export function useEditorSearch(editorRef: MutableRefObject<Ace.Editor | undefin
useEffect(() => {
const editor = editorRef.current;

if (!isOpen) cache.current = undefined;
if (!editor) return;

if (isOpen) showHighlight(editor);
Expand Down
16 changes: 11 additions & 5 deletions src/react/hooks/useMarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import {
readWholeWord,
TOKEN_STYLES,
} from "../../domain/Marks";
import {offsetToPosition, SearchResult} from "../../utils/TextSearch";
import {offsetToPosition, SearchDocument, SearchResult, searchText} from "../../utils/TextSearch";
import {logError} from "../../utils/Log";
import {addMatchLayers, goToTarget, MatchLayer, readSearchTerm, searchSession} from "../EditorSearch";
import {addMatchLayers, goToTarget, MatchLayer, readSearchDocument, readSearchTerm, searchSession} from "../EditorSearch";
import {markStateChanged} from "../StateChangeTracker";
import {useEditorContext} from "../components/editor/EditorContext";
import {EditorCommandHandlers} from "./useEditorCommands";
Expand All @@ -51,6 +51,7 @@ export function useMarks(editorRef: MutableRefObject<Ace.Editor | undefined>) {
const bookmarks = useRef<Array<number>>([]);
const smartHighlight = useRef<SearchQuery>();
const results = useRef(new Map<string, SearchResult>());
const searchDocument = useRef<SearchDocument>();
const isSmartHighlightOn = useRef(settings.search.smartHighlight);

isSmartHighlightOn.current = settings.search.smartHighlight;
Expand All @@ -66,13 +67,18 @@ export function useMarks(editorRef: MutableRefObject<Ace.Editor | undefined>) {
tab.current.state = {...tab.current.state, ...changes};
};

const forgetResults = () => {
results.current = new Map();
searchDocument.current = undefined;
};

const readLayers = (session: Ace.EditSession): Array<MatchLayer> => {
const previous = results.current;
const next = new Map<string, SearchResult>();

const layerOf = (query: SearchQuery, className: string): MatchLayer => {
const key = JSON.stringify(query);
const result = previous.get(key) ?? next.get(key) ?? searchSession(session, query);
const result = previous.get(key) ?? next.get(key) ?? searchText(searchDocument.current ??= readSearchDocument(session), query);

next.set(key, result);

Expand Down Expand Up @@ -118,7 +124,7 @@ export function useMarks(editorRef: MutableRefObject<Ace.Editor | undefined>) {
const session = editor.session;

tab.current = loadedTab;
results.current = new Map();
forgetResults();
smartHighlight.current = undefined;
rules.current = normalizeMarkRules(loadedTab.state?.marks);
bookmarks.current = [];
Expand All @@ -128,7 +134,7 @@ export function useMarks(editorRef: MutableRefObject<Ace.Editor | undefined>) {
session.on("change", delta => {
const shifted = shiftRows(bookmarks.current, delta);

results.current = new Map();
forgetResults();

if (shifted !== bookmarks.current) setBookmarks(session, shifted);
});
Expand Down
8 changes: 5 additions & 3 deletions src/utils/EditorUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "node:path";
import {createHash} from "node:crypto";
import {backupFile, getFileSize, isBinaryFile, isFileExists, readFileAsync, readFileBytes, writeFileAtomic} from "./FileUtils";
import {EditorTab} from "../domain/EditorTab";
import {documentHash, hash, shortenTabName} from "./TextUtils";
Expand Down Expand Up @@ -106,7 +107,7 @@ function normalizeTempTab(tab: EditorTab) {
if (tab.content !== undefined) tab.content = toEditorText(tab.content, tab.eol);
}

let lastWrittenState: string | undefined;
let lastWrittenStateHash: string | undefined;

export function writeEditorState(state: EditorState, discardUnsaved: boolean) {
state.tabs.forEach(t => {
Expand All @@ -119,12 +120,13 @@ export function writeEditorState(state: EditorState, discardUnsaved: boolean) {
});

const serialized = JSON.stringify(state, null, 4);
const stateHash = createHash("sha256").update(serialized).digest("hex");

if (serialized === lastWrittenState) return;
if (stateHash === lastWrittenStateHash) return;

writeFileAtomic(Environment.getStateFilePath(), serialized);

lastWrittenState = serialized;
lastWrittenStateHash = stateHash;
}

export const LARGE_FILE_WARNING_SIZE = 50 * 1024 * 1024;
Expand Down
8 changes: 5 additions & 3 deletions src/utils/SearchResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,15 @@ export function compactHits(document: SearchDocument, matches: Array<TextSpan>):
return {lines: lineNumbers.map(row => readLine(document, row)), lineNumbers: lineNumbers, hits: hits};
}

export function toCompactDocument({lines, lineNumbers, hits}: CompactHits): Pick<DocumentHits, "document" | "lineNumbers" | "hits"> {
return {document: createSearchDocument(lines), lineNumbers: lineNumbers, hits: hits};
}

export function toDocumentHits(matches: FileMatches, directory: string): DocumentHits {
return {
name: relativePath(directory, matches.file) ?? getFileName(matches.file),
file: matches.file,
document: createSearchDocument(matches.lines),
lineNumbers: matches.lineNumbers,
hits: matches.hits,
...toCompactDocument(matches),
};
}

Expand Down
12 changes: 11 additions & 1 deletion src/utils/__tests__/SearchResults.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {describe, expect, it} from "vitest";
import {DEFAULT_SEARCH_OPTIONS, SearchTarget} from "../../domain/Search";
import {applyProgress, compactHits, createResultRows, describeHit, findAdjacentHit, getHit, hitKey, previewMatch, SearchRun} from "../SearchResults";
import {applyProgress, compactHits, createResultRows, describeHit, findAdjacentHit, getHit, hitKey, previewMatch, SearchRun, toCompactDocument} from "../SearchResults";
import {createSearchDocument} from "../TextSearch";

const DOCUMENT = createSearchDocument(["a", "a", "a"]);
Expand Down Expand Up @@ -57,6 +57,16 @@ describe("SearchResults", () => {
]);
});

it("describes compacted hits like the hits of the whole document", () => {
const document = createSearchDocument(["first line", "skip", "the hit here", "skip", "hit", "spans", "last"]);
const matches = [{start: 20, end: 23}, {start: 34, end: 44}];
const whole = {name: "d", document: document, hits: matches};
const compact = {name: "d", ...toCompactDocument(compactHits(document, matches))};

expect(compact.document.text.length).toBeLessThan(document.text.length);
expect(compact.hits.map(hit => describeHit(compact, hit))).toEqual(matches.map(hit => describeHit(whole, hit)));
});

it("lists the rows of runs and skips collapsed parts", () => {
const runs = [run(2, [2, 1]), run(1, [1])];
const keys = (collapsed: Set<string>) => {
Expand Down
Loading