Commit f5c517ff authored by Miklos Magyari's avatar Miklos Magyari
Browse files

Code peek window sized to content; leading whitespace trimming


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent 1115c721
......@@ -23,6 +23,7 @@ import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
......@@ -71,9 +72,6 @@ public class Ttcn3SourceViewControl implements IInformationControl, IInformation
layout.marginHeight = 10;
layout.marginWidth = 10;
composite.setLayout(layout);
setSize(600, 400);
final Display display = myshell.getDisplay();
//setBackgroundColor(display.getSystemColor(SWT.COLOR_YELLOW));
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
composite.setLayoutData(gridData);
......@@ -103,6 +101,48 @@ public class Ttcn3SourceViewControl implements IInformationControl, IInformation
return;
}
/*
* We may need to strip some leading tabs/spaces from all lines except the first one
*
* This is because whitespaces are typically missing for the first line, and all the other
* lines are indented
*/
String[] lines = information.split("\n");
int minspaces = Integer.MAX_VALUE;
int spacecount = 0;
if (lines.length > 1) {
StringBuilder notabs = new StringBuilder();
for (int i = 1; i < lines.length; i++) {
spacecount = 0;
for (int j = 0; j < lines[i].length(); j++) {
if (lines[i].charAt(j) == '\t') {
spacecount += 4;
notabs.append(" ");
} else if (lines[i].charAt(j) == ' ') {
spacecount++;
notabs.append(" ");
} else {
notabs.append(lines[i].substring(j));
break;
}
}
notabs.append("\n");
if (spacecount < minspaces) {
minspaces = spacecount;
}
}
StringBuilder stripped = new StringBuilder();
if (minspaces != Integer.MAX_VALUE && minspaces > 0) {
stripped.append(lines[0]);
lines = notabs.toString().split("\n");
for (int i = 0; i < lines.length; i++) {
stripped.append("\n");
stripped.append(lines[i].substring(minspaces));
}
information = stripped.toString();
}
}
viewer.setText(information);
IDocument doc = new Document(information);
CodeScanner scanner = new CodeScanner(new ColorManager());
......@@ -127,6 +167,8 @@ public class Ttcn3SourceViewControl implements IInformationControl, IInformation
viewer.setStyleRange(range);
}
final Point size = computeSizeHint();
setSize(size.x, size.y);
}
@Override
......@@ -137,10 +179,8 @@ public class Ttcn3SourceViewControl implements IInformationControl, IInformation
@Override
public Point computeSizeHint() {
int x = SWT.DEFAULT;
int y = SWT.DEFAULT;
Point size = myshell.computeSize(x, y);
return size;
myshell.layout();
return myshell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
}
@Override
......@@ -150,7 +190,12 @@ public class Ttcn3SourceViewControl implements IInformationControl, IInformation
@Override
public void setSize(int width, int height) {
myshell.setSize(width, height);
myshell.layout();
Rectangle displayRect = myshell.getDisplay().getClientArea();
Rectangle shellRect = myshell.getBounds();
maxWidth = displayRect.width - shellRect.x;
maxHeight = displayRect.height - shellRect.y;
myshell.setSize(Math.min(width, maxWidth), Math.min(height, maxHeight));
}
@Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment