use of org.eclipse.ui.texteditor.ITextEditor in project linuxtools by eclipse.
the class FileHyperlink method linkActivated.
@Override
public void linkActivated() {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editorPart = null;
try {
editorPart = IDE.openEditorOnFileStore(page, file);
if (line > 0) {
ITextEditor textEditor = null;
if (editorPart instanceof ITextEditor) {
textEditor = (ITextEditor) editorPart;
} else {
textEditor = editorPart.getAdapter(ITextEditor.class);
}
if (textEditor != null) {
IEditorInput input = editorPart.getEditorInput();
IDocumentProvider provider = textEditor.getDocumentProvider();
try {
provider.connect(input);
} catch (CoreException e) {
// unable to link
return;
}
IDocument document = provider.getDocument(input);
int offset = -1;
int length = -1;
try {
IRegion region = document.getLineInformation(line - 1);
offset = region.getOffset();
length = region.getLength();
} catch (BadLocationException e) {
// unable to link
}
provider.disconnect(input);
if (offset >= 0 && length >= 0) {
textEditor.selectAndReveal(offset, length);
}
}
}
} catch (PartInitException e) {
// Put your exception handler here if you wish to
}
}
use of org.eclipse.ui.texteditor.ITextEditor in project linuxtools by eclipse.
the class GNUFormat method mergeChangelog.
@Override
public String mergeChangelog(String dateLine, String functionGuess, String defaultContent, IEditorPart changelog, String changeLogLocation, String fileLocation) {
String fileDetail = formatFileDetail(changeLogLocation, fileLocation);
IDocument changelog_doc = getDocument(changelog);
String function = formatFunction(functionGuess);
boolean multipleEntrySuccess = false;
boolean forceNewEntry = false;
// $NON-NLS-1$
String functionSpacer = " ";
if (// $NON-NLS-1$
function.equals(": "))
// $NON-NLS-1$
functionSpacer = "";
/* Fix Bz #366854. Make sure that forceNewEntry is used only
* once and then cleared even when the ChangeLog is empty to start with.
*/
if (changelog instanceof ChangeLogEditor) {
ChangeLogEditor editor = (ChangeLogEditor) changelog;
forceNewEntry = editor.isForceNewLogEntry();
editor.setForceNewLogEntry(false);
}
if (changelog_doc.getLength() > 0) {
int offset_start = findChangeLogEntry(changelog_doc, dateLine);
int offset_end = dateLine.length();
boolean foundFunction = false;
// written to a new entry again.
if (forceNewEntry) {
offset_start = -1;
}
if (offset_start != -1) {
int nextChangeEntry = findChangeLogPattern(changelog_doc, offset_start + dateLine.length());
int functLogEntry = offset_start + dateLine.length();
final int numLines = changelog_doc.getNumberOfLines();
while (functLogEntry < nextChangeEntry) {
int lineNum = 0;
// $NON-NLS-1$
String entry = "";
try {
lineNum = changelog_doc.getLineOfOffset(functLogEntry);
entry = changelog_doc.get(functLogEntry, changelog_doc.getLineLength(lineNum));
} catch (BadLocationException e) {
// Should never get here
}
// Look to see if entry already exists for file (will be preceded by "*")
final int entryStart = entry.indexOf("* " + fileDetail);
if (entryStart >= 0) {
foundFunction = true;
} else if (foundFunction && isFileLine(entry)) {
functLogEntry--;
break;
}
if (foundFunction) {
foundFunction = true;
// In such a case, just return. We don't need to repeat ourselves.
if (defaultContent.length() > 0 && entry.lastIndexOf(defaultContent) > 0) {
// $NON-NLS-1$
return "";
}
final int nextFunctLoc;
if (entryStart > 0) {
nextFunctLoc = functLogEntry + entryStart + fileDetail.length() + 2;
} else {
nextFunctLoc = functLogEntry;
}
// $NON-NLS-1$
String nextFunc = "";
try {
final int lineEnd;
if (lineNum < numLines - 1) {
lineEnd = changelog_doc.getLineOffset(lineNum + 1) - 1;
} else {
lineEnd = changelog_doc.getLength();
}
nextFunc = changelog_doc.get(nextFunctLoc, lineEnd - nextFunctLoc);
} catch (BadLocationException e1) {
// Should never get here
}
if (nextFunc.trim().startsWith(function)) {
// $NON-NLS-1$
return "";
}
}
try {
functLogEntry += changelog_doc.getLineLength(lineNum);
} catch (BadLocationException e1) {
// Should never get here
}
}
if (functLogEntry >= nextChangeEntry) {
functLogEntry = nextChangeEntry - 1;
try {
// Get rid of some potential lines containing whitespace only.
functLogEntry = removeWhitespaceOnlyLines(changelog_doc, functLogEntry);
while (// $NON-NLS-1$
changelog_doc.get(functLogEntry, 1).equals("\n")) functLogEntry--;
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
functLogEntry++;
}
if (offset_start != -1) {
if (foundFunction) {
try {
if (// $NON-NLS-1$
!function.equals(": "))
changelog_doc.replace(functLogEntry, 0, // $NON-NLS-1$
"\n" + TAB + function + // $NON-NLS-1$
" ");
else
// $NON-NLS-1$
changelog_doc.replace(// $NON-NLS-1$
functLogEntry, // $NON-NLS-1$
0, // $NON-NLS-1$
"\n" + TAB);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ITextEditor edit = (ITextEditor) changelog;
if (// $NON-NLS-1$
!function.equals(": "))
edit.selectAndReveal(functLogEntry + function.length() + 3, 0);
else
edit.selectAndReveal(functLogEntry + function.length(), 0);
multipleEntrySuccess = true;
} else {
try {
changelog_doc.replace(offset_end, 0, TAB + "* " + fileDetail + // $NON-NLS-1$
functionSpacer + function + functionSpacer + defaultContent + // $NON-NLS-1$
"\n");
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ITextEditor edit = (ITextEditor) changelog;
edit.selectAndReveal(offset_end + fileDetail.length() + function.length() + functionSpacer.length() * 2 + 3 + defaultContent.length(), 0);
multipleEntrySuccess = true;
}
}
}
}
if (!multipleEntrySuccess) {
try {
if (changelog_doc.getLength() > 0) {
// $NON-NLS-1$
changelog_doc.replace(0, 0, "\n\n");
}
changelog_doc.replace(0, 0, // $NON-NLS-1$
dateLine + TAB + "* " + fileDetail + functionSpacer + function + functionSpacer + defaultContent);
ITextEditor edit = (ITextEditor) changelog;
edit.selectAndReveal(dateLine.length() + fileDetail.length() + function.length() + functionSpacer.length() * 2 + 3 + defaultContent.length(), 0);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
// $NON-NLS-1$
return "";
}
use of org.eclipse.ui.texteditor.ITextEditor in project linuxtools by eclipse.
the class ProfileUIUtils method openEditorAndSelect.
/**
* Open a file in the Editor at the specified offset, highlighting the given length
*
* @param path : Absolute path pointing to the file which will be opened.
* @param line - line number to select, 0 to not select a line
* @param project - current project object
* @throws BadLocationException - Line number not valid in file
* @throws PartInitException if the editor could not be initialized
* @throws CoreException if the proxy cannot be initialized
* @since 3.1
*/
public static void openEditorAndSelect(String path, int line, IProject project) throws PartInitException, BadLocationException, CoreException {
IWorkbenchPage activePage = ProfileUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
IRemoteFileProxy proxy = null;
proxy = RemoteProxyManager.getInstance().getFileProxy(project);
IFileStore file = proxy.getResource(path);
if (file.fetchInfo().exists()) {
IEditorPart editor = IDE.openEditorOnFileStore(activePage, file);
if (editor instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) editor;
if (line > 0) {
IDocumentProvider provider = textEditor.getDocumentProvider();
IDocument document = provider.getDocument(textEditor.getEditorInput());
// zero-indexed
int start = document.getLineOffset(line - 1);
textEditor.selectAndReveal(start, 0);
}
}
}
}
use of org.eclipse.ui.texteditor.ITextEditor in project linuxtools by eclipse.
the class SpecfileEditorShowOutlineActionDelegate method execute.
@Override
public Object execute(ExecutionEvent event) {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
ITextEditor textEditor = editor.getAdapter(ITextEditor.class);
SpecfileQuickOutlineDialog quickOutlinePopupDialog = new SpecfileQuickOutlineDialog(editor.getSite().getShell(), SWT.NONE, textEditor);
quickOutlinePopupDialog.setVisible(true);
return null;
}
use of org.eclipse.ui.texteditor.ITextEditor in project linuxtools by eclipse.
the class SpecfileEditorToggleCommentActionDelegate method execute.
@Override
public Object execute(ExecutionEvent event) {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
ITextEditor specfileEditor = editor.getAdapter(ITextEditor.class);
IDocumentProvider dp = specfileEditor.getDocumentProvider();
IDocument document = dp.getDocument(specfileEditor.getEditorInput());
ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
if (currentSelection instanceof ITextSelection) {
ITextSelection selection = (ITextSelection) currentSelection;
// $NON-NLS-1$
String selectedContent = "";
try {
int begin = document.getLineOffset(selection.getStartLine());
StringBuilder sb = new StringBuilder(document.get().substring(0, begin));
String content = document.get().substring(begin, selection.getOffset() + selection.getLength());
if (linesContentCommentChar(content)) {
if (selection.getStartLine() == selection.getEndLine()) {
selectedContent = ISpecfileSpecialSymbols.COMMENT_START + content;
} else {
// $NON-NLS-1$ //$NON-NLS-2$
selectedContent = ISpecfileSpecialSymbols.COMMENT_START + content.replaceAll("\n", "\n#");
}
} else {
selectedContent = // $NON-NLS-1$
content.replaceFirst(ISpecfileSpecialSymbols.COMMENT_START, "").replaceAll("\n#", // $NON-NLS-1$ //$NON-NLS-2$
"\n");
}
sb.append(selectedContent);
sb.append(document.get().substring(selection.getOffset() + selection.getLength(), document.get().length()));
document.set(sb.toString());
specfileEditor.setHighlightRange(selection.getOffset(), selection.getLength(), true);
} catch (BadLocationException e) {
SpecfileLog.logError(e);
}
}
return null;
}
Aggregations