use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.
the class RevertSelectionAction method computeEnablement.
@Override
public boolean computeEnablement() {
if (!super.computeEnablement())
return false;
ITextSelection selection = getSelection();
if (selection == null)
return false;
fStartLine = selection.getStartLine();
fEndLine = selection.getEndLine();
// only enable if mouse activity is inside line range
int activityLine = getLastLine();
if (activityLine == -1 || activityLine < fStartLine || activityLine > fEndLine + 1)
// + 1 to cover the case where the selection goes to the offset of the next line
return false;
ILineDiffer differ = getDiffer();
if (differ == null)
return false;
// only enable if selection covers at least two lines
if (fEndLine > fStartLine) {
for (int i = fStartLine; i <= fEndLine; i++) {
ILineDiffInfo info = differ.getLineInfo(i);
if (info != null && info.hasChanges()) {
return true;
}
}
}
return false;
}
use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.
the class FileBufferOperationHandler method computeSelectedResources.
/**
* Computes the selected resources.
*/
protected final void computeSelectedResources() {
if (fResources != null || fLocation != null)
return;
ISelection selection = getSelection();
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
ArrayList<Object> resources = new ArrayList<>(structuredSelection.size());
Iterator<?> e = structuredSelection.iterator();
while (e.hasNext()) {
Object element = e.next();
if (element instanceof IResource)
resources.add(element);
else if (element instanceof IAdaptable) {
IAdaptable adaptable = (IAdaptable) element;
Object adapter = adaptable.getAdapter(IResource.class);
if (adapter instanceof IResource)
resources.add(adapter);
}
}
if (!resources.isEmpty())
fResources = resources.toArray(new IResource[resources.size()]);
} else if (selection instanceof ITextSelection) {
IWorkbenchWindow window = getWorkbenchWindow();
if (window != null) {
IWorkbenchPart workbenchPart = window.getPartService().getActivePart();
if (workbenchPart instanceof IEditorPart) {
IEditorPart editorPart = (IEditorPart) workbenchPart;
IEditorInput input = editorPart.getEditorInput();
Object adapter = input.getAdapter(IResource.class);
if (adapter instanceof IResource)
fResources = new IResource[] { (IResource) adapter };
else {
adapter = input.getAdapter(ILocationProvider.class);
if (adapter instanceof ILocationProvider) {
ILocationProvider provider = (ILocationProvider) adapter;
fLocation = provider.getPath(input);
}
}
}
}
}
}
use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.
the class RetrieverAction method extractSearchTextFromEditor.
protected final String extractSearchTextFromEditor(IEditorPart editor) {
if (editor != null) {
ITextSelection selection = null;
ISelectionProvider provider = editor.getEditorSite().getSelectionProvider();
if (provider != null) {
ISelection s = provider.getSelection();
if (s instanceof ITextSelection) {
selection = (ITextSelection) s;
}
}
if (selection != null) {
if (selection.getLength() == 0) {
ITextEditor txtEditor = getTextEditor(editor);
if (txtEditor != null) {
IDocument document = txtEditor.getDocumentProvider().getDocument(txtEditor.getEditorInput());
selection = expandSelection(selection, document, null);
}
}
if (selection.getLength() > 0 && selection.getText() != null) {
return trimSearchString(selection.getText());
}
}
}
return null;
}
use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.
the class RetrieverAction method expandSelection.
private ITextSelection expandSelection(ITextSelection sel, IDocument document, String stopChars) {
int offset = sel.getOffset();
int length = sel.getLength();
// left or right.
if (length == 0) {
// try right
char chr = 0;
char chl = 0;
try {
chr = document.getChar(offset);
} catch (BadLocationException e2) {
}
try {
chl = document.getChar(offset - 1);
} catch (BadLocationException e2) {
}
if (isPartOfIdentifier(chr)) {
length = 1;
} else if (isPartOfIdentifier(chl)) {
offset--;
length = 1;
} else if (stopChars != null && stopChars.indexOf(chr) == -1) {
length = 1;
} else if (stopChars != null && stopChars.indexOf(chl) == -1) {
offset--;
length = 1;
} else {
return sel;
}
}
int a = offset + length - 1;
int z = a;
// move z one behind last character.
try {
char ch = document.getChar(z);
while (isValidChar(stopChars, ch)) {
ch = document.getChar(++z);
}
} catch (BadLocationException e2) {
}
// move a one before the first character
try {
char ch = document.getChar(a);
while (isValidChar(stopChars, ch)) {
ch = document.getChar(--a);
}
} catch (BadLocationException e2) {
}
if (a == z) {
offset = a;
length = 0;
} else {
offset = a + 1;
length = z - a - 1;
}
return new TextSelection(document, offset, length);
}
use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.
the class TemplateCompletionProcessor method computeCompletionProposals.
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// adjust offset to end of normalized selection
if (selection.getOffset() == offset)
offset = selection.getOffset() + selection.getLength();
String prefix = extractPrefix(viewer, offset);
Region region = new Region(offset - prefix.length(), prefix.length());
TemplateContext context = createContext(viewer, region);
if (context == null)
return new ICompletionProposal[0];
// name of the selection variables {line, word}_selection //$NON-NLS-1$
context.setVariable("selection", selection.getText());
Template[] templates = getTemplates(context.getContextType().getId());
List<ICompletionProposal> matches = new ArrayList<>();
for (Template template : templates) {
try {
context.getContextType().validate(template.getPattern());
} catch (TemplateException e) {
continue;
}
if (template.matches(prefix, context.getContextType().getId()))
matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
}
Collections.sort(matches, fgProposalComparator);
return matches.toArray(new ICompletionProposal[matches.size()]);
}
Aggregations