use of org.eclipse.jface.text.source.IVerticalRulerInfo in project linuxtools by eclipse.
the class AddStapProbeHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
ITextEditor editor;
try {
editor = (ITextEditor) HandlerUtil.getActiveEditor(event);
} catch (ClassCastException e) {
ExceptionErrorDialog.openError(Messages.AddStapProbe_unableToInsertProbe, Messages.AddStapProbe_editorError, e);
throw new ExecutionException(Messages.AddStapProbe_editorError, e);
}
IVerticalRulerInfo rulerInfo = editor.getAdapter(IVerticalRulerInfo.class);
Shell shell = editor.getSite().getShell();
shell.setCursor(shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT));
int lineno = rulerInfo.getLineOfLastMouseButtonActivity();
IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
String s = document.get();
// $NON-NLS-1$
String[] lines = s.split("\n");
String line = lines[lineno].trim();
boolean die = false;
if (line.isEmpty()) {
// eat blank lines
die = true;
}
if (line.startsWith("#")) {
// eat preprocessor directives //$NON-NLS-1$
die = true;
}
if (line.startsWith("//")) {
// eat C99 comments //$NON-NLS-1$
die = true;
}
if (line.startsWith("/*") && !line.contains("*/") && !line.endsWith("*/")) {
// try to eat single-line C comments //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
die = true;
}
// gogo find comment segments
try {
ArrayList<Integer> commentChunks = new ArrayList<>();
char[] chars = s.toCharArray();
int needle = 1;
int offset = document.getLineOffset(lineno);
while (needle < chars.length) {
if (chars[needle - 1] == '/' && chars[needle] == '*') {
commentChunks.add(needle);
while (needle < chars.length) {
if (chars[needle - 1] == '*' && chars[needle] == '/') {
commentChunks.add(needle);
needle++;
break;
}
needle++;
}
}
needle++;
}
for (int i = 0, pair, start, end; i < commentChunks.size(); i++) {
if (!(commentChunks.get(i).intValue() < offset)) {
pair = i - i % 2;
start = commentChunks.get(pair).intValue();
end = commentChunks.get(pair + 1).intValue();
if (offset >= start && offset <= end) {
die = true;
}
}
}
} catch (BadLocationException excp) {
ExceptionErrorDialog.openError(Messages.AddStapProbe_unableToInsertProbe, excp);
return null;
}
if (die) {
MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.CEditor_probeInsertFailed, Messages.CEditor_canNotProbeLine);
} else {
IEditorInput in = editor.getEditorInput();
if (in instanceof FileStoreEditorInput) {
FileStoreEditorInput input = (FileStoreEditorInput) in;
IPreferenceStore p = IDEPlugin.getDefault().getPreferenceStore();
String kernroot = p.getString(IDEPreferenceConstants.P_KERNEL_SOURCE);
String filepath = input.getURI().getPath();
String kernrelative = filepath.substring(kernroot.length() + 1, filepath.length());
StringBuffer sb = new StringBuffer();
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
sb.append("probe kernel.statement(\"*@" + kernrelative + ":" + (lineno + 1) + "\")");
// $NON-NLS-1$
sb.append("\n{\n\t\n}\n");
STPEditor activeSTPEditor = IDESessionSettings.getOrAskForActiveSTPEditor(false);
if (activeSTPEditor != null) {
activeSTPEditor.insertText(sb.toString());
}
}
}
// Return the cursor to normal
shell.setCursor(null);
return null;
}
use of org.eclipse.jface.text.source.IVerticalRulerInfo in project eclipse.platform.text by eclipse.
the class AbstractRulerActionDelegate method setActiveEditor.
@Override
public void setActiveEditor(IAction callerAction, IEditorPart targetEditor) {
if (fEditor != null) {
IVerticalRulerInfo rulerInfo = fEditor.getAdapter(IVerticalRulerInfo.class);
if (rulerInfo != null) {
Control control = rulerInfo.getControl();
if (control != null && !control.isDisposed())
control.removeMouseListener(this);
}
if (fEditor instanceof ITextEditorExtension)
((ITextEditorExtension) fEditor).removeRulerContextMenuListener(this);
}
fEditor = targetEditor == null ? null : targetEditor.getAdapter(ITextEditor.class);
fCallerAction = callerAction;
fAction = null;
if (fEditor != null) {
if (fEditor instanceof ITextEditorExtension)
((ITextEditorExtension) fEditor).addRulerContextMenuListener(this);
IVerticalRulerInfo rulerInfo = fEditor.getAdapter(IVerticalRulerInfo.class);
if (rulerInfo != null) {
fAction = createAction(fEditor, rulerInfo);
update();
Control control = rulerInfo.getControl();
if (control != null && !control.isDisposed())
control.addMouseListener(this);
}
}
}
Aggregations