Skip to content

Commit

Permalink
Utilise private static class methods to relocate some functions that …
Browse files Browse the repository at this point in the history
…were previously in the global front-end scope.
  • Loading branch information
mhutchie committed Oct 11, 2020
1 parent 162dd32 commit e18d7bb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
30 changes: 21 additions & 9 deletions web/findWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,28 @@ class FindWidget {
break;
}
if (matchEnd !== match.index) {
if (matchStart !== matchEnd) textElem.parentNode!.insertBefore(createFindMatchElem(text.substring(matchStart, matchEnd)), textElem);
// This match isn't immediately after the previous match, or isn't at the beginning of the text
if (matchStart !== matchEnd) {
// There was a previous match, insert it in a text node
textElem.parentNode!.insertBefore(FindWidget.createMatchElem(text.substring(matchStart, matchEnd)), textElem);
}
// Insert a text node containing the text between the last match and the current match
textElem.parentNode!.insertBefore(document.createTextNode(text.substring(matchEnd, match.index)), textElem);
matchStart = match.index;
}
matchEnd = findGlobalPattern.lastIndex;
}
if (matchEnd > 0) {
if (matchStart !== matchEnd) textElem.parentNode!.insertBefore(createFindMatchElem(text.substring(matchStart, matchEnd)), textElem);
// There were one or more matches
if (matchStart !== matchEnd) {
// There was a match, insert it in a text node
textElem.parentNode!.insertBefore(FindWidget.createMatchElem(text.substring(matchStart, matchEnd)), textElem);
}
if (matchEnd !== text.length) {
// There was some text after last match, update the textElem (the last node of it's parent) to contain the remaining text.
textElem.textContent = text.substring(matchEnd);
} else {
// The last match was at the end of the text, the textElem is no longer required, so delete it
textElem.parentNode!.removeChild(textElem);
}
}
Expand All @@ -225,7 +236,7 @@ class FindWidget {
if (colVisibility.commit && commit.hash.search(findPattern) === 0 && !findPattern.test(abbrevCommit(commit.hash)) && textElems.length > 0) {
// The commit matches on more than the abbreviated commit, so the commit should be highlighted
let commitNode = textElems[textElems.length - 1]; // Commit is always the last column if it is visible
commitNode.parentNode!.replaceChild(createFindMatchElem(commitNode.textContent!), commitNode);
commitNode.parentNode!.replaceChild(FindWidget.createMatchElem(commitNode.textContent!), commitNode);
}
if (zeroLengthMatch) break;
}
Expand Down Expand Up @@ -304,11 +315,12 @@ class FindWidget {
if (this.matches.length === 0) return;
this.updatePosition(this.position < this.matches.length - 1 ? this.position + 1 : 0, true);
}
}

function createFindMatchElem(text: string) {
let span = document.createElement('span');
span.className = CLASS_FIND_MATCH;
span.innerHTML = text;
return span;
private static createMatchElem(text: string) {
const span = document.createElement('span');
span.className = CLASS_FIND_MATCH;
span.innerHTML = text;
return span;
}

}
30 changes: 15 additions & 15 deletions web/settingsWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ class SettingsWidget {
html += '<table style="display:inline-table; width:360px; text-align:left; font-size:12px; margin-bottom:2px;"><tr><td>Issue Regex:</td><td>#(\\d+)</td></tr><tr><td>Issue URL:</td><td>https://github.com/mhutchie/repo/issues/$1</td></tr></tbody></table>';

if (!isEdit && defaultIssueRegex === null && defaultIssueUrl === null) {
defaultIssueRegex = autoDetectIssueRegex(this.view.getCommits());
defaultIssueRegex = SettingsWidget.autoDetectIssueRegex(this.view.getCommits());
if (defaultIssueRegex !== null) {
html += '<p style="font-size:12px"><i>The prefilled Issue Regex was detected in commit messages in this repository. Review and/or correct it if necessary.</i></p>';
}
Expand Down Expand Up @@ -729,20 +729,20 @@ class SettingsWidget {
? this.settings.remotes[parseInt((<HTMLElement>(<Element>e.target).closest('.remoteBtns')!).dataset.index!)]
: null;
}
}

function autoDetectIssueRegex(commits: ReadonlyArray<GG.GitCommit>) {
const patterns = ['#(\\d+)', '^(\\d+)\\.(?=\\s|$)', '^(\\d+):(?=\\s|$)', '([A-Za-z]+-\\d+)'].map((pattern) => {
const regexp = new RegExp(pattern);
return {
pattern: pattern,
matches: commits.filter((commit) => regexp.test(commit.message)).length
};
}).sort((a, b) => b.matches - a.matches);

if (patterns[0].matches > 0.1 * commits.length) {
// If the most common pattern was matched in more than 10% of commits, return the pattern
return patterns[0].pattern;
private static autoDetectIssueRegex(commits: ReadonlyArray<GG.GitCommit>) {
const patterns = ['#(\\d+)', '^(\\d+)\\.(?=\\s|$)', '^(\\d+):(?=\\s|$)', '([A-Za-z]+-\\d+)'].map((pattern) => {
const regexp = new RegExp(pattern);
return {
pattern: pattern,
matches: commits.filter((commit) => regexp.test(commit.message)).length
};
}).sort((a, b) => b.matches - a.matches);

if (patterns[0].matches > 0.1 * commits.length) {
// If the most common pattern was matched in more than 10% of commits, return the pattern
return patterns[0].pattern;
}
return null;
}
return null;
}

0 comments on commit e18d7bb

Please sign in to comment.