use of org.eclipse.jface.viewers.StyledString in project eclipse.platform.text by eclipse.
the class FileLabelProvider method getLineElementLabel.
private StyledString getLineElementLabel(LineElement lineElement) {
int lineNumber = lineElement.getLine();
String lineNumberString = Messages.format(SearchMessages.FileLabelProvider_line_number, Integer.valueOf(lineNumber));
StyledString str = new StyledString(lineNumberString, StyledString.QUALIFIER_STYLER);
FileMatch[] matches = lineElement.getMatches(fPage.getInput());
Arrays.sort(matches, fMatchComparator);
String content = lineElement.getContents();
int pos = evaluateLineStart(matches, content, lineElement.getOffset());
int length = content.length();
// number of characters to leave away if the line is too long
int charsToCut = getCharsToCut(length, matches);
for (int i = 0; i < matches.length; i++) {
FileMatch match = matches[i];
int start = Math.max(match.getOriginalOffset() - lineElement.getOffset(), 0);
// append gap between last match and the new one
if (pos < start) {
if (charsToCut > 0) {
charsToCut = appendShortenedGap(content, pos, start, charsToCut, i == 0, str);
} else {
str.append(content.substring(pos, start));
}
}
// append match
int end = Math.min(match.getOriginalOffset() + match.getOriginalLength() - lineElement.getOffset(), lineElement.getLength());
str.append(content.substring(start, end), DecoratingFileSearchLabelProvider.HIGHLIGHT_STYLE);
pos = end;
}
// append rest of the line
if (charsToCut > 0) {
appendShortenedGap(content, pos, length, charsToCut, false, str);
} else {
str.append(content.substring(pos));
}
return str;
}
use of org.eclipse.jface.viewers.StyledString in project eclipse.platform.text by eclipse.
the class FileLabelProvider method getStyledText.
@Override
public StyledString getStyledText(Object element) {
if (element instanceof LineElement)
return getLineElementLabel((LineElement) element);
if (!(element instanceof IResource))
return new StyledString();
IResource resource = (IResource) element;
if (!resource.exists())
new StyledString(SearchMessages.FileLabelProvider_removed_resource_label);
String name = BasicElementLabels.getResourceName(resource);
if (fOrder == SHOW_LABEL) {
return getColoredLabelWithCounts(resource, new StyledString(name));
}
String pathString = BasicElementLabels.getPathLabel(resource.getParent().getFullPath(), false);
if (fOrder == SHOW_LABEL_PATH) {
StyledString str = new StyledString(name);
String decorated = Messages.format(fgSeparatorFormat, new String[] { str.getString(), pathString });
StyledCellLabelProvider.styleDecoratedString(decorated, StyledString.QUALIFIER_STYLER, str);
return getColoredLabelWithCounts(resource, str);
}
StyledString str = new StyledString(Messages.format(fgSeparatorFormat, new String[] { pathString, name }));
return getColoredLabelWithCounts(resource, str);
}
use of org.eclipse.jface.viewers.StyledString in project eclipse.platform.text by eclipse.
the class CompletionProposalPopup method handleSetData.
/*
* @since 3.1
*/
private void handleSetData(Event event) {
TableItem item = (TableItem) event.item;
int index = fProposalTable.indexOf(item);
if (0 <= index && index < fFilteredProposals.size()) {
ICompletionProposal current = fFilteredProposals.get(index);
String displayString;
StyleRange[] styleRanges = null;
Image image = null;
try {
if (fIsColoredLabelsSupportEnabled && current instanceof ICompletionProposalExtension7 && Helper.okToUse(fProposalShell)) {
BoldStylerProvider boldStylerProvider = fContentAssistant.getBoldStylerProvider();
if (boldStylerProvider == null) {
boldStylerProvider = new BoldStylerProvider(fProposalShell.getFont());
fContentAssistant.setBoldStylerProvider(boldStylerProvider);
}
StyledString styledString = ((ICompletionProposalExtension7) current).getStyledDisplayString(fContentAssistSubjectControlAdapter.getDocument(), fFilterOffset, boldStylerProvider);
displayString = styledString.getString();
styleRanges = styledString.getStyleRanges();
} else if (fIsColoredLabelsSupportEnabled && current instanceof ICompletionProposalExtension6) {
StyledString styledString = ((ICompletionProposalExtension6) current).getStyledDisplayString();
displayString = styledString.getString();
styleRanges = styledString.getStyleRanges();
} else {
displayString = current.getDisplayString();
}
} catch (RuntimeException e) {
// On failures to retrieve the proposal's text, insert a dummy entry and log the error.
// $NON-NLS-1$
displayString = JFaceTextMessages.getString("CompletionProposalPopup.error_retrieving_proposal");
// $NON-NLS-1$
String PLUGIN_ID = "org.eclipse.jface.text";
ILog log = Platform.getLog(Platform.getBundle(PLUGIN_ID));
// $NON-NLS-1$
log.log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, JFaceTextMessages.getString("CompletionProposalPopup.unexpected_error"), e));
}
try {
image = current.getImage();
} catch (RuntimeException e) {
// If we are unable to retrieve the proposal's image, leave it blank.
}
item.setText(displayString);
if (fIsColoredLabelsSupportEnabled)
TableOwnerDrawSupport.storeStyleRanges(item, 0, styleRanges);
item.setImage(image);
item.setData(current);
} else {
// this should not happen, but does on win32
}
}
use of org.eclipse.jface.viewers.StyledString in project linuxtools by eclipse.
the class StyledConsoleLogsTest method shouldGenerateStyledString.
@Test
public void shouldGenerateStyledString() {
// given
// when
final StyledString result = StyledTextBuilder.parse(lineText);
// then
assertThat(SWTUtils.syncExec(() -> result.getStyleRanges())).isEqualTo(SWTUtils.syncExec(() -> expectedStyledString.getStyleRanges()));
assertThat(result.getString()).isEqualTo(expectedStyledString.getString());
}
use of org.eclipse.jface.viewers.StyledString in project linuxtools by eclipse.
the class DockerComposeConsole method writeContentInConsole.
private void writeContentInConsole(final String text) {
Display.getDefault().asyncExec(() -> {
final StyledString styledString = StyledTextBuilder.parse(text);
final StyleRange[] styleRanges = styledString.getStyleRanges();
Stream.of(styleRanges).forEach(range -> {
try (final IOConsoleOutputStream consoleStream = newOutputStream()) {
consoleStream.setColor(range.foreground);
consoleStream.write(styledString.getString().substring(range.start, range.start + range.length).getBytes());
} catch (IOException e) {
Activator.log(e);
}
});
});
}
Aggregations