Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit c4cb376

Browse files
authored
Add code intelligence to embedded snippets (#45)
1 parent 62eb6ca commit c4cb376

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

app/components/BlobAnnotator.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface Props {
1616
basePath: string | null;
1717
repoURI: string;
1818
fileElement: HTMLElement;
19+
rev?: string;
1920
}
2021

2122
interface State {
@@ -32,6 +33,7 @@ export class BlobAnnotator extends React.Component<Props, State> {
3233
isCommit?: boolean;
3334
isPullRequest?: boolean;
3435
isSplitDiff?: boolean;
36+
isCodePage?: boolean;
3537

3638
// rev is defined for blob view
3739
rev?: string;
@@ -50,7 +52,7 @@ export class BlobAnnotator extends React.Component<Props, State> {
5052

5153
this.fileExtension = utils.getPathExtension(props.headPath);
5254

53-
const { isDelta, isPullRequest, isCommit } = github.parseURL(window.location);
55+
const { isDelta, isPullRequest, isCommit, isCodePage } = github.parseURL(window.location);
5456
let { rev } = github.parseURL(window.location);
5557
const gitHubState = github.getGitHubState(window.location.href);
5658
// TODO(uforic): Eventually, use gitHubState for everything, but for now, only use it when the branch should have a
@@ -62,7 +64,8 @@ export class BlobAnnotator extends React.Component<Props, State> {
6264
this.isDelta = isDelta;
6365
this.isPullRequest = isPullRequest;
6466
this.isCommit = isCommit;
65-
this.rev = rev;
67+
this.isCodePage = isCodePage;
68+
this.rev = this.props.rev || rev;
6669

6770
if (this.isDelta) {
6871
this.isSplitDiff = github.isSplitDiff();
@@ -208,6 +211,9 @@ export class BlobAnnotator extends React.Component<Props, State> {
208211
}
209212

210213
render(): JSX.Element | null {
214+
if (!this.isCodePage) {
215+
return null;
216+
}
211217
if (!this.isDelta && !Boolean(this.state.resolvedRevs[this.props.repoURI])) {
212218
return null;
213219
}

app/github/util.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,8 @@ export function parseURL(loc: Location = window.location): GitHubURL {
558558

559559
return { user, repo, rev, path, repoURI, uri: repoURI, isDelta, isPullRequest, isCommit, isCodePage };
560560
}
561+
562+
// Code Comments
563+
export function getCodeCommentContainers(): HTMLCollectionOf<HTMLElement> {
564+
return document.getElementsByClassName("js-comment-body") as HTMLCollectionOf<HTMLElement>;
565+
}

chrome/extension/inject/github.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ function inject(): void {
106106
injectRepositorySearchToggle();
107107
injectOpenOnSourcegraphButton();
108108
injectBlobAnnotators();
109+
injectCodeSnippetAnnotator();
109110
injectGitHubEditor();
110111
injectFileTree();
111112
}
@@ -277,6 +278,31 @@ function selectTreeNodeForURL(): void {
277278
tree.select_node(gitHubState["path"]);
278279
}
279280

281+
function injectCodeSnippetAnnotator(): void {
282+
const codeComments = github.getCodeCommentContainers();
283+
284+
for (const file of Array.from(codeComments)) {
285+
const mountEl = document.createElement("div");
286+
mountEl.style.display = "none";
287+
mountEl.className = "sourcegraph-app-annotator";
288+
file.appendChild(mountEl);
289+
const code = file.querySelector(".border.rounded-1.my-2");
290+
const filePathContainer = file.querySelector(".mb-0.text-bold");
291+
if (!filePathContainer) {
292+
continue;
293+
}
294+
const href = filePathContainer.firstElementChild ? (filePathContainer.firstElementChild as any).href : undefined;
295+
if (!href) {
296+
continue;
297+
}
298+
const gitHubState = github.getGitHubState(href);
299+
if (!gitHubState || !gitHubState.repo || !gitHubState.rev || !gitHubState["path"]) {
300+
continue;
301+
}
302+
render(<BlobAnnotator headPath={gitHubState["path"]} repoURI={`github.com/${gitHubState.owner}/${gitHubState.repo}`} fileElement={code as HTMLElement} basePath={null} rev={gitHubState.rev} />, mountEl);
303+
}
304+
}
305+
280306
function injectBlobAnnotators(): void {
281307
const { repoURI, isDelta } = github.parseURL();
282308
let { path } = github.parseURL();

0 commit comments

Comments
 (0)