Skip to content

Commit

Permalink
inline chat fixes
Browse files Browse the repository at this point in the history
* workaround weird CSS padding
* fix strings#rcut
* tweak cancel and toggle keybindings
  • Loading branch information
jrieken committed Jan 24, 2025
1 parent c4c7c61 commit f5b8c4c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
19 changes: 9 additions & 10 deletions src/vs/base/common/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,21 +786,20 @@ export function rcut(text: string, n: number, suffix = ''): string {
return trimmed;
}

const re = /\b/g;
let lastWordBreak = trimmed.length;

while (re.test(trimmed)) {
if (trimmed.length - re.lastIndex > n) {
lastWordBreak = re.lastIndex;
const parts = text.split(/\b/);
let result = '';
for (const part of parts) {
if (result.length > 0 && result.length + part.length > n) {
break;
}
re.lastIndex += 1;
result += part;
}

if (lastWordBreak === trimmed.length) {
return trimmed;
if (result === trimmed) {
return result;
}

return (trimmed.substring(0, lastWordBreak) + suffix).trimEnd();
return result.trim().replace(/b$/, '') + suffix;
}

// Escape codes, compiled from https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
Expand Down
18 changes: 18 additions & 0 deletions src/vs/base/test/common/strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ suite('Strings', () => {
assert.strictEqual(strings.lcut('............a', 10, '…'), '............a');
});

suite('rcut', () => {
test('basic truncation', () => {
assert.strictEqual(strings.rcut('foo bar', 0), 'foo');
assert.strictEqual(strings.rcut('foo bar', 1), 'foo');
assert.strictEqual(strings.rcut('foo bar', 4), 'foo');
assert.strictEqual(strings.rcut('foo bar', 7), 'foo bar');
assert.strictEqual(strings.rcut('test string 0.1.2.3', 3), 'test');
});

test('truncation with suffix', () => {
assert.strictEqual(strings.rcut('foo bar', 0, '…'), 'foo…');
assert.strictEqual(strings.rcut('foo bar', 1, '…'), 'foo…');
assert.strictEqual(strings.rcut('foo bar', 4, '…'), 'foo…');
assert.strictEqual(strings.rcut('foo bar', 7, '…'), 'foo bar');
assert.strictEqual(strings.rcut('test string 0.1.2.3', 3, '…'), 'test…');
});
});

test('escape', () => {
assert.strictEqual(strings.escape(''), '');
assert.strictEqual(strings.escape('foo'), 'foo');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { IInlineChatSession2, IInlineChatSessionService } from './inlineChatSess
import { InlineChatZoneWidget } from './inlineChatZoneWidget.js';


export const CTX_HAS_SESSION = new RawContextKey<boolean>('inlineChatHasSession', undefined, localize('chat.hasInlineChatSession', "The current editor has an active inline chat session"));
export const CTX_HAS_SESSION = new RawContextKey<undefined | 'empty' | 'active'>('inlineChatHasSession', undefined, localize('chat.hasInlineChatSession', "The current editor has an active inline chat session"));


export class InlineChatController2 implements IEditorContribution {
Expand Down Expand Up @@ -103,6 +103,8 @@ export class InlineChatController2 implements IEditorContribution {
this._editor
);

zone.domNode.classList.add('inline-chat-2');

const overlay = ChatEditorOverlayController.get(_editor)!;

const editorObs = observableCodeEditor(_editor);
Expand All @@ -117,9 +119,16 @@ export class InlineChatController2 implements IEditorContribution {
});


this._store.add(autorun(r => {
this._store.add(autorunWithStore((r, store) => {
const session = sessionObs.read(r);
ctxHasSession.set(Boolean(session));

if (!session) {
ctxHasSession.set(undefined);
} else {
const checkRequests = () => ctxHasSession.set(session.chatModel.getRequests().length === 0 ? 'empty' : 'active');
store.add(session.chatModel.onDidChange(checkRequests));
checkRequests();
}
}));

const visibleSessionObs = observableValue<IInlineChatSession2 | undefined>(this, undefined);
Expand Down Expand Up @@ -166,9 +175,8 @@ export class InlineChatController2 implements IEditorContribution {
zone.widget.setChatModel(session.chatModel);
if (!zone.position) {
zone.show(session.initialPosition);
} else {
zone.reveal(zone.position);
}
zone.reveal(zone.position!);
zone.widget.focus();
session.editingSession.getEntry(session.uri)?.autoAcceptController.get()?.cancel();
}
Expand Down Expand Up @@ -303,12 +311,11 @@ export class StopSessionAction2 extends AbstractInlineChatAction {
id: 'inlineChat2.stop',
title: localize2('stop', "Stop"),
f1: true,
precondition: ContextKeyExpr.and(
CTX_HAS_SESSION, CTX_INLINE_CHAT_VISIBLE
),
precondition: ContextKeyExpr.and(CTX_HAS_SESSION.isEqualTo('empty'), CTX_INLINE_CHAT_VISIBLE),
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyCode.Escape
primary: KeyCode.Escape,
secondary: [KeyMod.CtrlCmd | KeyCode.KeyI]
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
outline: none;
}

.monaco-workbench .inline-chat .chat-widget .interactive-session .interactive-input-part {
.monaco-workbench .zone-widget.inline-chat-widget:not(.inline-chat-2) .inline-chat .chat-widget .interactive-session .interactive-input-part {
padding: 4px 0 0 0;
}

Expand Down

0 comments on commit f5b8c4c

Please sign in to comment.