Skip to content

Code highlight in message #226

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

brichet
Copy link
Collaborator

@brichet brichet commented Jun 10, 2025

This PR add code highlighting when it is attached from a cell or a selection.

image

The language is added to the markdown code block when it is available.

  • Code attached from a cell
    Get the language from the defaultKernelLanguage of the Notebook metadata. If not defined, there is no syntax highlighting.

  • Code attached from a selection
    Retrieve the DocumentWidget mimetype from its editor model, and convert it to a language using the EditorLanguageRegistry.
    If the mimetype cannot be converted to a language,there is no syntax highlighting.

Context

Fixes #222
Related to jupyterlab/jupyter-ai#1356

Copy link
Contributor

Binder 👈 Launch a Binder on branch brichet/jupyter-chat/code_highlight_in_message

@brichet brichet added the bug Something isn't working label Jun 10, 2025
@brichet brichet marked this pull request as ready for review June 10, 2025 13:45
@brichet brichet requested a review from andrii-i June 10, 2025 13:46
@brichet brichet force-pushed the code_highlight_in_message branch from a4c51db to 3cdca00 Compare June 12, 2025 07:45
@brichet brichet force-pushed the code_highlight_in_message branch from 3cdca00 to b4ef27a Compare June 12, 2025 08:42
Copy link
Collaborator

@andrii-i andrii-i 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 working on this @brichet. I tested this PR locally and it works well. It also improves user experience as missing code highlighting in the snippets attached to the chat messages was noticeable.

Please find small suggestion on making typing of language property more explicit: #226 (comment).

@@ -100,19 +100,22 @@ export function SendButton(

function sendWithSelection() {
let source = '';

let language = '';
Copy link
Collaborator

@andrii-i andrii-i Jun 13, 2025

Choose a reason for hiding this comment

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

If we to follow suggestion in #226 (comment), || '' part is not needed, this should be declared as

let language: string | undefined;

}
let content = model.value;
if (source) {
content += `

\`\`\`
\`\`\`${language}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check for if language is undefined and replacement with empty string could be for example done here via something like language ?? ''

@@ -148,6 +150,11 @@ export class ActiveCellManager implements IActiveCellManager {
getContent(withError: true): CellWithErrorContent | null;
getContent(withError = false): CellContent | CellWithErrorContent | null {
const sharedModel = this._notebookTracker.activeCell?.model.sharedModel;
const language =
Copy link
Collaborator

Choose a reason for hiding this comment

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

For maintainability, clarity, and to potentially prevent misinterpretation from contributors and bugs would it make sense to use undefined to explicitly represent "not set" (then '' could explicitly represent an empty string, for example set by a user)?

source = activeCellManager.getContent(false)!.source;
const content = activeCellManager.getContent(false);
source = content!.source;
language = content?.language || '';
Copy link
Collaborator

@andrii-i andrii-i Jun 13, 2025

Choose a reason for hiding this comment

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

If we to follow suggestion in #226 (comment), || '' part is not needed

/**
* The language of the selection.
*/
language: string;
Copy link
Collaborator

@andrii-i andrii-i Jun 13, 2025

Choose a reason for hiding this comment

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

If we to follow suggestion in #226 (comment), language could / should be declared as optional.

@@ -207,13 +225,15 @@ function getTextSelection(
[start, end] = [end, start];
}

const language = await languages.getLanguage(editor?.model.mimeType);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note getLanguage return type is Promise<IEditorLanguage | null> so it also returns "not set" in an explicit manner but using null.

return {
...selectionObj,
start,
end,
text,
numLines: text.split('\n').length,
widgetId: widget.id,
language: language?.name || '',
Copy link
Collaborator

@andrii-i andrii-i Jun 13, 2025

Choose a reason for hiding this comment

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

If we to follow suggestion in #226 (comment), if language is null it does not need to be returned at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code attached from the cell has no code highlighting in the chat windows
2 participants