diff --git a/com.microsoft.copilot.eclipse.ui/src/com/microsoft/copilot/eclipse/ui/chat/SourceViewerComposite.java b/com.microsoft.copilot.eclipse.ui/src/com/microsoft/copilot/eclipse/ui/chat/SourceViewerComposite.java index b661f77b..b1e737a8 100644 --- a/com.microsoft.copilot.eclipse.ui/src/com/microsoft/copilot/eclipse/ui/chat/SourceViewerComposite.java +++ b/com.microsoft.copilot.eclipse.ui/src/com/microsoft/copilot/eclipse/ui/chat/SourceViewerComposite.java @@ -213,6 +213,24 @@ private Button createActionButton(Composite parent, int style, Image image, Stri return result; } + @Override + public Point computeSize(int widthHint, int heightHint, boolean changed) { + if (this.sourceViewer == null) { + return super.computeSize(widthHint, heightHint, changed); + } + + StyledText textWidget = this.sourceViewer.getTextWidget(); + if (textWidget == null || textWidget.isDisposed()) { + return super.computeSize(widthHint, heightHint, changed); + } + + Point textSize = textWidget.computeSize(widthHint, SWT.DEFAULT, changed); + Rectangle trim = computeTrim(0, 0, textSize.x, getSourceViewerHeight(textWidget, textSize)); + int width = widthHint == SWT.DEFAULT ? trim.width : widthHint; + int height = heightHint == SWT.DEFAULT ? trim.height : heightHint; + return new Point(Math.max(0, width), Math.max(0, height)); + } + private void refreshScrollerLayout() { if (this.sourceViewer == null) { return; @@ -224,16 +242,20 @@ private void refreshScrollerLayout() { } Point size = textWidget.computeSize(SWT.DEFAULT, SWT.DEFAULT); Rectangle clientArea = this.getClientArea(); - // remove scroll-bar height - ScrollBar horizontalBar = textWidget.getHorizontalBar(); - int scrollbarHeight = horizontalBar != null ? horizontalBar.getSize().y : 0; - int height = size.y - scrollbarHeight; + int height = getSourceViewerHeight(textWidget, size); // Set bounds on SourceViewer's control (the direct child), not just the textWidget this.sourceViewer.getControl().setBounds(0, 0, clientArea.width, height); textWidget.redraw(); }); } + private int getSourceViewerHeight(StyledText textWidget, Point textSize) { + // remove scroll-bar height + ScrollBar horizontalBar = textWidget.getHorizontalBar(); + int scrollbarHeight = horizontalBar != null ? horizontalBar.getSize().y : 0; + return Math.max(0, textSize.y - scrollbarHeight); + } + private void insert(Event e) { String content = this.sourceViewer.getDocument().get(); if (StringUtils.isNotEmpty(content)) {