Howto: Eclipse plugin Textmarker hightlight by line not by char and delete them - eclipse

I write my own Eclipse-Plugin and get an errorlog from a server.
This log includes linenumbers. I want to mark this lines with another backround color if you click on the Error in my View. My Problem i can only mark text in my editor with CHAR_START and CHAR_END but i dont know how many characters are in each line. The line number is totally ignorer And how can i delete this markers in my code? In my example the first 10 characters are marked irrespective of the line I set
IEditorPart editorPart = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()
.getActiveEditor();
if (editorPart != null) {
FileEditorInput input = (FileEditorInput) editorPart
.getEditorInput();
IFile file = input.getFile();
IMarker marker = null;
try {
marker = file.createMarker("de.fhduesseldorf.medien.mi.codecheck.marker");
marker.setAttribute(IMarker.LINE_NUMBER, 4);
marker.setAttribute(IMarker.CHAR_START, 0);
marker.setAttribute(IMarker.CHAR_END, 10);
} catch (CoreException e) {
e.printStackTrace();
}
}

You have to get the text content being edited and manually count characters (including new line chars) in each line until you get to the line in question. That's the only way to determine the character offsets of a given line or lines.

Related

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
}

TextMarginFinder to verify printability

I am attempting to use TextMarginFinder to prove that odd and even pages back up correctly when printing. I have based my code on:
http://itextpdf.com/examples/iia.php?id=280
The issue I have is that on odd pages I am looking for the box to be aligned to the left showing a 1CM back margin for example, and on an even page I would expect the page box to be aligned to the right also showing a 1CM back margin. Even in the example above this is not the case, but when printed the text does back up perfectly because the Trim Box conforms.
In summary I believe on certain PDF files the TextMarginFinder is incorrectly locating the text width, usually on Even pages. This is evident by the width being greater than the actual text. This is usually the case if there are slug marks outside of the Media Box area.
In the PDF the OP pointed to (margins.pdf from the iText samples themselves) indeed the box is not flush with the text:
If you look into the PDF Content, though, you'll see that many of the lines have a trailing space character, e.g. the first line:
(s I have worn out since I started my ) Tj
These trailing space characters are part of the text and, therefore, the box does not flush with the visible text but it does with the text including such space characters.
If you want to ignore such space characters, you can try doing so by filtering such trailing spaces (or for the sake of simplicity all spaces) before they get fed into the TextMarginFinder. To do this I'd explode the TextRenderInfo instances character-wise and then filter those which trim to empty strings.
A helper class to explode the render info objects:
import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
public class TextRenderInfoSplitter implements RenderListener
{
public TextRenderInfoSplitter(RenderListener strategy) {
this.strategy = strategy;
}
public void renderText(TextRenderInfo renderInfo) {
for (TextRenderInfo info : renderInfo.getCharacterRenderInfos()) {
strategy.renderText(info);
}
}
public void beginTextBlock() {
strategy.beginTextBlock();
}
public void endTextBlock() {
strategy.endTextBlock();
}
public void renderImage(ImageRenderInfo renderInfo) {
strategy.renderImage(renderInfo);
}
final RenderListener strategy;
}
Using this helper you can update the iText sample like this:
RenderFilter spaceFilter = new RenderFilter() {
public boolean allowText(TextRenderInfo renderInfo) {
return renderInfo != null && renderInfo.getText().trim().length() > 0;
}
};
PdfReader reader = new PdfReader(src);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
TextMarginFinder finder = new TextMarginFinder();
FilteredRenderListener filtered = new FilteredRenderListener(finder, spaceFilter);
parser.processContent(i, new TextRenderInfoSplitter(filtered));
PdfContentByte cb = stamper.getOverContent(i);
cb.rectangle(finder.getLlx(), finder.getLly(), finder.getWidth(), finder.getHeight());
cb.stroke();
}
stamper.close();
reader.close();
The result:
In case of slug area text etc you might want to filter more, e.g. anything outside the crop box.
Beware, though, there might be fonts in which the space character is not invisible, e.g. a font of boxed characters. Taking the spaces out of the equation in that case would be wrong.

Howto Highlight Code Background Color by line

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.

Eclipse: selection autocopy to clipboard

I love an Emacs feature to copy selection to clipboard automatically. Is it possible to do the same on Eclipse?
Environment: Windows XP, Helios
To copy a String from Eclipse to the clipboard, you can use
void copyToClipboard (String toClipboard, Display display){
String toClipboard = "my String";
Clipboard clipboard = new Clipboard(display);
TextTransfer [] textTransfer = {TextTransfer.getInstance()};
clipboard.setContents(new Object [] {toClipboard}, textTransfer);
clipboard.dispose();
}
Then you can call this method from a MouseAdapter or KeyAdapter, depending on where you want to get your String from. In your case it could be MouseAdapter, which listens to doubleclicks, gets the current cursor position of the text, marks the word and then adds the String to the clipboard.
edit to answer a question: You can set up your own MouseAdapater and attach it to buttons, text fields or whateer you like. Here's an example for a button:
Button btnGo1 = new Button(parent, SWT.NONE);
btnGo1.setText("Go");
btnGo1.addMouseListener(new MouseAdapter() {
#Override
public void mouseDoubleClick(MouseEvent e) {
//do what you want to do in here
}
});
If you want to implement mouseUp and mouseDown events, too, you can just add MouseListenerinstead of the Adapter. The only advantage of the Adapter is, that you don't have to override the other methods of the interface.
Since the original question was to automatically get the selection of the text of an editor: the way to get the selection from an editor is explained here.
You can try this plugin. Along with auto copy points mentioned in Eclipse show number of lines and/or file size also addressed.