Howto Highlight Code Background Color by line - eclipse

I am working on a eclipse plugin and trying to highlight parts of the Code with a backround color. Is there any option to highlight to change the backround color in the editor by line numbers.
For Example:
editor.highlightLines(2,5,"red"); I i
There are so much informations when i googled for it. Annotations, Makers etc. but noone could give me a Tutorial or a link to and API which can do what i need. I only got informations for syntax highlighting, but i want to highlights parts of the code by line number.

You can do this as long as the editor implements ITextEditor:
IEditorPart editorPart = page.openEditor(new FileEditorInput(file), getEditorId() , true);
ITextEditor textEditor;
if (editorPart instanceof ITextEditor) {
textEditor = (ITextEditor)editorPart;
} else {
textEditor = (ITextEditor)editorPart.getAdapter(ITextEditor.class);
}
IEditorInput input = editorPart.getEditorInput();
IDocumentProvider provider = textEditor.getDocumentProvider();
provider.connect(input);
IDocument document = provider.getDocument(input);
IRegion region = document.getLineInformation(lineNumber - 1);
int fileOffset = region.getOffset();
int fileLength = region.getLength();
provider.disconnect(input);
textEditor.selectAndReveal(fileOffset, fileLength);
(extracted from org.eclipse.debug.ui.console.FileLink which the console code uses). I have left out many error checks of exception catches.

Related

Open DialectEditor programmatically outside of main editor area (E3/E4 hybrid)

what I want to do:
In my RCP an E3/E4 hybrid I have a project and library based on sirius tree. The User can drag an drop item from the library tree to the project tree. This works fine and was no great problem to build in. So now I want to make the UI more usable. It should looks like this layout:
what works:
After application startup I open my library presentation with the DialectUIManager.
final DialectEditor editor = (DialectEditor)
DialectUIManager.INSTANCE.openEditor(siriusSession, description, monitor);
Okay, this works. But it open it in the editor in the part market as org.eclipse.ui.editorss. This it not what I want
what does not work:
I want to show it in the "Library Part". I can move it manually with the mouse after open the editor, but how can i tell DialectUIManager to open it direct there. Or how can I programmatically it move there.
I do a lot of google research but i don't found a solution. The only thing I found was a hint Pierre-Charles David https:// www. eclipse.org/forums/index.php?t=msg&th=998476&goto=1631138&#msg_1631138
If you need is simply to show the editor outside of the main editor
area, this is possible since Eclipse 4.2 (e4 does not really treat the
main editor area as something special), so you can have your editor
"around" another editor in the middle of other views.
But at this step I stuck. I also ask it in the Sirius Forum but they say its a Eclipse E4 problem
Thanks for help, code snippets or links to correct part of manual.
I've found a solution. It's not very nice, but it works. I execute these code here after the editors have opened.
What the code does:
He is looking for the MPlaceholder which has the ID: org. eclipse. ui. editorss. There he descends until he is with the parts. These are in the Compatibly editor mode. Then he chooses the part we wants to move out of and Attach them to the MPartStack target.
public static void movePart(MApplication application,
EModelService modelService) {
MPart partToMove = null;
MUIElement muiElement =
modelService.find("org.eclipse.ui.editorss", application);
if (muiElement instanceof MPlaceholder) {
MPlaceholder placeholder = (MPlaceholder) muiElement;
MUIElement ref = placeholder.getRef();
if (ref instanceof MArea) {
MArea area = (MArea) ref;
List<MPartSashContainerElement> children = area.getChildren();
for (MPartSashContainerElement mPartSashContainerElement
: children) {
if (mPartSashContainerElement instanceof MPartStack) {
MPartStack partStack = (MPartStack) mPartSashContainerElement;
List<MStackElement> children2 = partStack.getChildren();
for (MStackElement mStackElement : children2) {
if (mStackElement instanceof MPart) {
MPart part = (MPart) mStackElement;
// Library is the Editor Name wiche I want to move
if (part.getLabel().equals("Library")) {
partToMove = part;
break;
}
}
}
}
}
}
}
if (partToMove != null) {
moveElement(modelService, application, partToMove);
}
}
private static void moveElement(EModelService modelService,
MApplication application, MPart part) {
// target PartStack
MUIElement find = modelService.find("de.bsg.onesps.rcp.
partstack.library", application);
if (find instanceof MPartStack) {
MPartStack mPartStack = (MPartStack) find;
mPartStack.getChildren().add(part);
mPartStack.setSelectedElement(part);
}
}

Eclipse Editor - SWT StyledText CaretListener offset not corresponding to real file line number

I am currently working on an Eclipse plugin. In order to do an action, I need to listen to the caret listener of the active tab.
public void partOpened(IWorkbenchPartReference partRef) {
AbstractTextEditor e = (AbstractTextEditor) ((IEditorReference) partRef).getEditor(false);
StyledText sText = ((StyledText) e.getAdapter(Control.class));
sText.addCaretListener(new CaretListener() {
#Override
public void caretMoved(CaretEvent event) {
IDocument d = e.getDocumentProvider().getDocument(e.getEditorInput());
...
int line = d.getLineOfOffset(event.caretOffset);
Point p = sText.getLocationAtOffset(event.caretOffset);
}
});
}
I use this code to add the CaretListener on the latest opened tab.
The variable line is correct only when no code blocks are collapsed.
In fact, the offset returned by the event is linked to the StyledText, but I'd like to get the line number of the file.
This picture shows an example of folded text. The StyledText caret offset will give me something like line 6, 7 and 8, instead of 6, 7 and 12 (like Eclipse does).
Is there a way to "transform" the StyledText offset to a "real file" offset ? I could retrieve the line as a String and find it in the file, but it sounds like a bad idea.
Thanks !
For folding editors the editor's source viewer will implement ITextViewerExtension5 which provides a widgetOffset2ModelOffset method to make this adjustment.
Get the caret position using something like:
ISourceViewer sourceViewer = e.getSourceViewer();
int caret;
if (sourceViewer instanceof ITextViewerExtension5) {
ITextViewerExtension5 extension = (ITextViewerExtension5)sourceViewer;
caret = extension.widgetOffset2ModelOffset(styledText.getCaretOffset());
} else {
int offset = sourceViewer.getVisibleRegion().getOffset();
caret = offset + styledText.getCaretOffset();
}

Custom go to line dialog eclipse

Is it possible to subclass the Go To Line dialog of the eclipse.
I want to create a similar dialog "Go To Index" with a custom "OK" action.
The Go to Line dialog is an inner class of org.eclipse.ui.texteditor.GotoLineAction so it cannot be subclassed.
However it is just an extension of InputDialog and the code to actually move to a line is simply:
int line = .... line number ...
ITextEditor editor = getTextEditor();
IDocumentProvider provider = editor.getDocumentProvider();
IDocument document = provider.getDocument(editor.getEditorInput());
try {
int start = document.getLineOffset(line);
editor.selectAndReveal(start, 0);
} catch (BadLocationException x) {
// ignore
}

Grab selected text from Eclipse Java editor

I'm developing an Eclipse plug-in where upon pressing a button, the plug-in takes the selected text in the Java editor and puts in a text box which appears.
My code looks like this: I got it from here: http://dev.eclipse.org/newslists/news.eclipse.newcomer/msg02200.html
private ITextSelection getSelection(ITextEditor editor) {
ISelection selection = editor.getSelectionProvider()
.getSelection();
return (ITextSelection) selection;
}
private String getSelectedText(ITextEditor editor) {
return getSelection(editor).getText();
}
The problem is how will I get the ITextEditor of the Java editor being displayed. Coincidentally it's the next question in the thread in the link I posted but it's unanswered :(
You could ask for the ActiveEditor, as in this thread:
IEditorPart part;
part =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().get
ActiveEditor();
if(part instanceof ITextEditor){
ITextEditor editor = (ITextEditor)part;
IDocumentProvider provider = editor.getDocumentProvider();
IDocument document = provider.getDocument(editor.getEditorInput());
The OP Krt_Malta mentions this blog entry "Programmatically query current text selection", which is similar to this other SO answer (written before the blog entry) "Replace selected code from eclipse editor through plugin command".
I'd like to add one thing to VonCs answer. The technique he describes to get the selection is useful for all kinds of text editors, not only Java editors as this questions is about. But his solution does not work in the case that the workspace part is a MultiPageEditorPart, since that is not a ITextEditor.
But in many cases (for example with the standard XML editor) a MultiPageEditorPart has pages which are ITextEditors. In those cases you can get the active page from a MultiPageEditorPart and get the selection from that.
This can be done with the following code:
ITextEditor editor = null;
if (part instanceof ITextEditor) {
editor = (ITextEditor) part;
} else if (part instanceof MultiPageEditorPart) {
Object page = ((MultiPageEditorPart) part).getSelectedPage();
if (page instanceof ITextEditor) editor = (ITextEditor) page;
}
if (editor != null) {
IDocumentProvider provider = editor.getDocumentProvider();
IDocument document = provider.getDocument(editor.getEditorInput());
}

get ITextViewer from IEDitorPart (Eclipse)

Eclipse RCP question
I open file with:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editorPart = IDE.openEditor(page, file);
I also get document with:
IDocument doc = ((ITextEditor)editorPart).getDocumentProvider().getDocument(editorPart.getEditorInput());
I need to get to text viewer of that document (for creating LinkedModeUI), is there any way to do this?
The following worked for me:
IEditorPart editorPart = getSite().getPage().getActiveEditor();
if (editorPart != null) {
ITextOperationTarget target =
(ITextOperationTarget)editorPart.getAdapter(ITextOperationTarget.class);
if (target instanceof ITextViewer) {
ITextViewer textViewer = (ITextViewer)target;
// ...
}
}
1) One document can be opened with more than one editor. You'll have to iterate all editors to look for your file's editors.
2) Viewer is encapsulated in editor. I think the only way is extend editor class to add getter. Or redefine it, if viewer is inaccessible from inheritors.