Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Outline matches #1091

Merged
merged 6 commits into from Nov 4, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions public/js/lib/codemirror-mozilla.css
Expand Up @@ -125,6 +125,13 @@ selector in floating-scrollbar-light.css across all platforms. */
background-repeat: repeat-x;
}

.cm-selecting {
background: none;
outline-width: 1px;
outline-style: solid;
outline-color: var(--theme-comment);
}

/* CodeMirror dialogs styling */

.CodeMirror-dialog {
Expand Down
46 changes: 44 additions & 2 deletions public/js/utils/source-search.js
Expand Up @@ -12,6 +12,7 @@
*/
function SearchState() {
this.posFrom = this.posTo = this.query = null;
this.overlay = null;
}

/**
Expand All @@ -32,6 +33,47 @@ function getSearchCursor(cm, query, pos) {
typeof query == "string" && query == query.toLowerCase());
}

/**
* This returns a mode object used by CoeMirror's addOverlay function
* to parse and style tokens in the file.
* The mode object contains a tokenizer function (token) which takes
* a character stream as input, advances it past a token, and returns
* a style for that token. For more details see
* https://codemirror.net/doc/manual.html#modeapi
*
* @memberof utils/source-search
* @static
*/
function searchOverlay(query) {
// escape special characters
query = query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol. this PR is crazy town, do you have some context on how you came up with it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a look into an example of how code mirror does highlighting of text. Basically Codemirror's addOverlay function takes a mode object. The mode object should have a token function which is applied as each token in the file is parsed. https://codemirror.net/doc/manual.html#option_mode

The regex just escapes any special characters in the input

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should i add as comments to the code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - i think that would help

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool .. will do!

query = new RegExp(query === "" ? "(?!\s*.*)" : query, "g");
return {
token: function(stream) {
query.lastIndex = stream.pos;
let match = query.exec(stream.string);
if (match && match.index == stream.pos) {
stream.pos += match[0].length || 1;
return "selecting";
} else if (match) {
stream.pos = match.index;
} else {
stream.skipToEnd();
}
}
};
}

/**
* @memberof utils/source-search
* @static
*/
function startSearch(cm, state, query) {
cm.removeOverlay(state.overlay);
state.overlay = searchOverlay(query);
cm.addOverlay(state.overlay, { opaque: true });
}

/**
* If there's a saved search, selects the next results.
* Otherwise, creates a new search and selects the first
Expand All @@ -53,7 +95,7 @@ function doSearch(ctx, rev, query) {
if (state.query) {
return;
}

startSearch(cm, state, query);
state.query = query;
state.posFrom = state.posTo = { line: 0, ch: 0 };
searchNext(ctx, rev);
Expand Down Expand Up @@ -100,7 +142,7 @@ function clearSearch(cm) {
if (!state.query) {
return;
}

cm.removeOverlay(state.overlay);
state.query = null;
}

Expand Down