sindresorhus/refined-github

Do you want to work on this issue?

You can request for a bounty in order to promote it!

Autocompletion for commit SHA #1547

stefanbuck posted onGitHub

When I work on code review comments, I often refer to a commit where this particular issue was solved. This requires manually copy & pasting the commit sha and this really annoys me.

Wouldn't it be cool, if you could trigger an autocomplete prompt for commit sha like you can do for GitHub users with the @-Sign?

image


@fregante

Help wanted to reverse engineer the existing @ or # autocompletion

An example of a custom autocompletion.


async function apiRequest(text) {
  const values = ["value", "value1", "value2", "test1", "value2"];

  return values.filter((value) =>
    value.toLowerCase().includes(text.toLowerCase())
  );
}

async function searchSHA(text) {
  const autocompleteValues = await apiRequest(text);

  const fragment = document.createElement("ul");
  fragment.setAttribute("role", "listbox");
  fragment.className =
    "suggester-container suggester suggestions list-style-none position-absolute";

  fragment.innerHTML = autocompleteValues
    .map(
      (
        value,
        i
      ) => `<li role="option" id="suggester-example-${i}" data-value="${value}">
    <span>${value}</span>
    <small>${value}</small>
  </li>`
    )
    .join("");

  return {
    matched: autocompleteValues.length > 0,
    fragment,
  };
}

const textExpander = document.querySelector("text-expander");
textExpander.setAttribute("keys", `${textExpander.getAttribute("keys")} !`);
textExpander.addEventListener("text-expander-change", (event) => {
  const { key, provide, text } = event.detail;
  if (key !== "!") return;

  provide(searchSHA(text));
});
textExpander.addEventListener("text-expander-value", (e) => {
  if ("!" !== e.detail.key) return;

  e.detail.value = `--__${e.detail.item.dataset.value}__--`;
});

Peek 2020-08-20 03-18

posted by artusm over 4 years ago

Not too bad. Do we have to handle the list generation and API rate limiting ourselves? I thought that the UI would at least be handled by GitHub

posted by fregante over 4 years ago

That looks correct according to https://github.com/github/text-expander-element :tada:

I suppose your code is only missing the api.v4 call to get the commit list. Also it can use JSX to generate the fragment effortlessly

posted by fregante over 4 years ago

Also it can use JSX to generate the fragment effortlessly

I know. It was just an example.

I suppose your code is only missing the api.v4 call to get the commit list

event listener (text-expander-change) added from Content Scripts - detail: null Screenshot from 2020-09-06 00-22-28 event listener (text-expander-change) added from console - detail: {...} Screenshot from 2020-09-06 00-28-06 It looks like we can't access to event.detail from Content Scripts (Only in Chrome).

Update: It works in Firefox Screenshot from 2020-09-06 01-03-01

posted by artusm over 4 years ago

That's likely due to isolation. I thought events were somewhat safe from this…

We worked around this for resolve-conflict, even if it's not pretty.

posted by fregante over 4 years ago

Fund this Issue

$0.00
Funded
Only logged in users can fund an issue

Pull requests