Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error field and animation in inline completion #15344

Merged
merged 10 commits into from
May 14, 2024

Conversation

Wzixiao
Copy link
Contributor

@Wzixiao Wzixiao commented Nov 1, 2023

References

Fixes #15343

Code changes

packages/completer/src/ghost.ts
const ERROR_INDICATOR_CLASS = 'jp-GhostText-errorIndicator';

export interface IGhostText {
  /**
   * An error occurred in the request.
   */
  isError?: boolean;
}

class GhostTextWidget{
 /**
  * Delete error animation.
  */
  private _removeErrorAnimation(dom: HTMLElement): void;
  
 /**
  * Mount the error animation dom and delete stream animation ghost dom.
  */
  private _mountErrorAnimation(dom: HTMLElement) : void;
  
  private _updateDOM(dom: HTMLElement) {
    if (this.options.isError) {
      this._mountErrorAnimation(dom);

      setTimeout(() => {
        this._removeErrorAnimation(dom);
      }, 2000);
      return;
    }
    ...
  }
}
packages/completer/src/inline.ts
export class InlineCompleter extends Widget {
  private _setText(item: CompletionHandler.IInlineItem) {
   ... 
    this._ghostManager.placeGhost(view, {
      from: editor.getOffsetAt(model.cursor),
      content: text,
      providerId: item.provider.identifier,
      addedPart: item.lastStreamed,
      streaming: item.streaming,
      onPointerOver: this._onPointerOverGhost.bind(this),
      onPointerLeave: this._onPointerLeaveGhost.bind(this),
      // new added
      isError: item.isError
    });
  }
}
packages/completer/src/reconciliator.ts
export class ProviderReconciliator implements IProviderReconciliator {
  private async _stream(
    item: CompletionHandler.IInlineItem,
    provider: IInlineCompletionProvider
  ) {
    ...
    for await (const reply of provider.stream(token)) {
      const updated = reply.response;
      const addition = updated.insertText.substring(item.insertText.length);
      item.insertText = updated.insertText;
      item.lastStreamed = addition;
      // new added
      item.isError = reply.response.isError;
      streamed.emit(CompletionHandler.StraemEvent.update);
    }
    ...
  }
}
packages/completer/src/tokens.ts
export interface IInlineCompletionItem extends IInlineCompletionItemLSP {
  token?: string;
  /**
   * Whether generation of `insertText` is still ongoing. If your provider supports streaming,
   * you can set this to true, which will result in the provider's `stream()` method being called
   * with `token` which has to be set for incomplete completions.
   */
  isIncomplete?: boolean;

  /**
   * This field is marked when an error occurs during a stream or fetch request.
   */
  isError?: boolean;
}
packages/completer/style/base.css
.jp-GhostText-errorIndicator::after {
  animation: jp-GhostText-error 2s infinite;
  animation-delay: 400ms;
  content: ' ';
  background: rgba(255, 0, 0, 0.5);
}

@keyframes jp-GhostText-error {
  0% {
    opacity: 0.5;
  }

  100% {
    opacity: 0;
  }
}

User-facing changes

deom_error_animation.mp4

Copy link

Thanks for making a pull request to jupyterlab!
To try out this branch on binder, follow this link: Binder

Copy link

welcome bot commented Nov 1, 2023

Thanks for submitting your first pull request! You are awesome! 🤗

If you haven't done so already, check out Jupyter's Code of Conduct. Also, please make sure you followed the pull request template, as this will help us review your contribution more quickly.
welcome
You can meet the other Jovyans by joining our Discourse forum. There is also a intro thread there where you can stop by and say Hi! 👋

Welcome to the Jupyter community! 🎉

Copy link
Member

@krassowski krassowski left a comment

Choose a reason for hiding this comment

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

Thank you for starting this @Wzixiao. Please see a few suggestions/questions below.

packages/completer/style/base.css Outdated Show resolved Hide resolved
packages/completer/src/ghost.ts Outdated Show resolved Hide resolved
packages/completer/src/ghost.ts Outdated Show resolved Hide resolved
packages/completer/src/tokens.ts Outdated Show resolved Hide resolved
@@ -127,6 +127,7 @@ export class ProviderReconciliator implements IProviderReconciliator {
// Stream an update.
item.insertText = updated.insertText;
item.lastStreamed = addition;
item.isError = reply.response.isError;
Copy link
Member

Choose a reason for hiding this comment

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

Does it also need to be set in fetchInline?

@krassowski krassowski added this to the 4.1.0 milestone Nov 1, 2023
@krassowski
Copy link
Member

@Wzixiao are you interested in finishing this PR? Our merge window for 4.1 will close soon.

@krassowski krassowski self-assigned this Dec 20, 2023
@krassowski krassowski modified the milestones: 4.1.0, 4.2.0 Jan 22, 2024
@krassowski krassowski modified the milestones: 4.2.0, 4.3.0 Apr 17, 2024
@krassowski
Copy link
Member

How to test:

  1. Checkout this PR
  2. Modify the history provider by adding , error: {message: 'Completion error!'} in line 102:
    items.push({
    insertText: followingLines
    });
  3. Rebuild, start JupyterLab in dev mode
  4. Enable history provider
  5. Type something in a cell which is a prefix of previously run command in your history

completion-error-indicator

Copy link
Member

@krassowski krassowski left a comment

Choose a reason for hiding this comment

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

This is good to go from my side - thank you @Wzixiao for starting this! I will wait a while before merging in case if anyone else wants to review the latest version (but preferably further enhancements would be tracked in separate issues and addressed in a follow-up PRs).

Copy link
Member

@fcollonval fcollonval left a comment

Choose a reason for hiding this comment

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

thanks @Wzixiao and @krassowski

@krassowski krassowski merged commit 0d1bb84 into jupyterlab:main May 14, 2024
82 checks passed
Copy link

welcome bot commented May 14, 2024

Congrats on your first merged pull request in this project! 🎉
congrats
Thank you for contributing, we are very proud of you! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design System CSS enhancement pkg:completer tag:CSS For general CSS related issues and pecadilloes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add request error animation in inline completion
3 participants