use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.
the class JSPTemplateCompletionProcessor method computeCompletionProposals.
/*
* Copied from super class except instead of calling createContext(viewer,
* region) call createContext(viewer, region, offset) instead
*/
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());
// If there's no prefix, check if we're in a tag open region
if (prefix.trim().length() == 0) {
IndexedRegion treeNode = ContentAssistUtils.getNodeAt(viewer, offset);
if (treeNode instanceof IDOMText) {
IDOMNode node = (IDOMNode) treeNode;
// Check each region in the node, if the offset is after a tag region, replace it with the template
IStructuredDocumentRegion cursor = node.getFirstStructuredDocumentRegion();
IStructuredDocumentRegion end = node.getLastStructuredDocumentRegion();
do {
if (cursor != null && DOMRegionContext.XML_TAG_OPEN.equals(cursor.getType()) && cursor.getStartOffset() == offset - 1) {
// We have a tag to replace
offset = cursor.getStartOffset();
region = new Region(cursor.getStartOffset(), cursor.getLength());
break;
}
} while (cursor != end && (cursor = cursor.getNext()) != null);
}
}
TemplateContext context = createContext(viewer, region, offset);
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 matches = new ArrayList();
for (int i = 0; i < templates.length; i++) {
Template template = templates[i];
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 (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
}
use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.
the class AddImportHandler method execute.
/* (non-Javadoc)
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
final IEditorSite site = HandlerUtil.getActiveEditor(event).getEditorSite();
final ISelectionProvider provider = site.getSelectionProvider();
final ISelection selection = provider != null ? provider.getSelection() : null;
if (selection instanceof IStructuredSelection && selection instanceof ITextSelection) {
final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
final int offset = ((ITextSelection) selection).getOffset();
final Object firstElement = structuredSelection.getFirstElement();
if (firstElement instanceof IDOMNode) {
final IDOMModel model = ((IDOMNode) firstElement).getModel();
INodeAdapter adapter = model.getDocument().getAdapterFor(IJSPTranslation.class);
if (adapter != null) {
JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
final JSPTranslationExtension translation = translationAdapter.getJSPTranslation();
translation.reconcileCompilationUnit();
final ICompilationUnit cu = translation.getCompilationUnit();
CompilationUnit astRoot = SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_YES, null);
if (astRoot != null) {
final ASTNode node = NodeFinder.perform(astRoot, translation.getJavaOffset(offset), 0);
if (node != null) {
SimpleName name = null;
if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
name = (SimpleName) node;
} else if (node.getNodeType() == ASTNode.QUALIFIED_NAME) {
name = ((QualifiedName) node).getName();
}
if (name != null) {
IBinding binding = name.resolveBinding();
if (binding instanceof ITypeBinding && (binding.getJavaElement() == null || !binding.getJavaElement().exists())) {
// Look it up!
ITypeBinding typeBinding = (ITypeBinding) binding;
final IImportContainer importContainer = cu.getImportContainer();
if (!importContainer.getImport(typeBinding.getQualifiedName()).exists()) {
final List typesFound = new ArrayList();
final TypeNameMatchRequestor collector = new TypeNameMatcher(typesFound);
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { cu.getJavaProject() });
final int mode = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
try {
new SearchEngine().searchAllTypeNames(null, mode, name.getIdentifier().toCharArray(), mode, IJavaSearchConstants.CLASS_AND_INTERFACE, scope, collector, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
final int length = typesFound.size();
final List elements = new ArrayList();
for (int i = 0; i < length; i++) {
final TypeNameMatch match = (TypeNameMatch) typesFound.get(i);
final int modifiers = match.getModifiers();
if (!Flags.isPrivate(modifiers) && !Flags.isPackageDefault(modifiers)) {
elements.add(match);
}
}
TypeNameMatch match = null;
// If there's only one match, insert it; otherwise, open the dialog to choose from the list
if (elements.size() == 1) {
match = (TypeNameMatch) elements.get(0);
} else if (elements.size() > 1) {
ElementListSelectionDialog dialog = new ElementListSelectionDialog(site.getShell(), LABEL_PROVIDER);
dialog.setElements(elements.toArray(new TypeNameMatch[elements.size()]));
dialog.setTitle(JSPUIMessages.AddImportHandler_title);
dialog.setMessage(JSPUIMessages.AddImportHandler_label);
if (dialog.open() == Window.OK) {
final Object result = dialog.getFirstResult();
if (result instanceof TypeNameMatch) {
match = (TypeNameMatch) result;
}
}
}
addImport(match, model.getStructuredDocument());
} catch (JavaModelException e) {
// $NON-NLS-1$
Logger.logException("Exception while determining import.", e);
}
}
}
}
}
}
}
}
}
return null;
}
use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.
the class JSPJavaSelectionProvider method getSelection.
static IJavaElement[] getSelection(ITextEditor textEditor) {
IJavaElement[] elements = null;
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
ISelection selection = textEditor.getSelectionProvider().getSelection();
if (selection instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) selection;
// get the JSP translation object for this editor's document
IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
try {
if (model instanceof IDOMModel) {
IDOMModel xmlModel = (IDOMModel) model;
IDOMDocument xmlDoc = xmlModel.getDocument();
JSPTranslationAdapter adapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
if (adapter != null) {
JSPTranslation translation = adapter.getJSPTranslation();
elements = translation.getElementsFromJspRange(textSelection.getOffset(), textSelection.getOffset() + textSelection.getLength());
}
}
} finally {
if (model != null)
model.releaseFromRead();
}
}
if (elements == null) {
elements = new IJavaElement[0];
}
return elements;
}
use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.
the class AbstractCommentActionXMLDelegate method run.
public void run(IAction action) {
if (fEditor instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) fEditor;
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
if (document != null) {
// get current text selection
ITextSelection textSelection = getCurrentSelection();
if (textSelection.isEmpty()) {
return;
}
processAction(document, textSelection);
}
}
}
use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.
the class CleanupdocumentHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
ITextEditor textEditor = null;
if (editor instanceof ITextEditor)
textEditor = (ITextEditor) editor;
else {
Object o = editor.getAdapter(ITextEditor.class);
if (o != null)
textEditor = (ITextEditor) o;
}
if (textEditor != null) {
final ITextEditor finalTextEditor = textEditor;
Dialog cleanupDialog = new CleanupDialogXML(editor.getSite().getShell());
if (cleanupDialog.open() == Window.OK) {
// setup runnable
Runnable runnable = new Runnable() {
public void run() {
IStructuredCleanupProcessor cleanupProcessor = getCleanupProcessor();
if (cleanupProcessor != null) {
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForEdit(finalTextEditor.getDocumentProvider().getDocument(finalTextEditor.getEditorInput()));
if (model != null) {
cleanupProcessor.cleanupModel(model);
}
} finally {
if (model != null) {
model.releaseFromEdit();
}
}
}
}
};
// TODO: make independent of 'model'.
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForEdit(textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()));
if (model != null) {
// begin recording
ITextSelection selection = (ITextSelection) textEditor.getSelectionProvider().getSelection();
model.beginRecording(this, SSEUIMessages.Cleanup_Document_UI_, SSEUIMessages.Cleanup_Document_UI_, selection.getOffset(), selection.getLength());
// tell the model that we are about to make a big
// model change
model.aboutToChangeModel();
// run
BusyIndicator.showWhile(textEditor.getEditorSite().getWorkbenchWindow().getShell().getDisplay(), runnable);
}
} finally {
if (model != null) {
// tell the model that we are done with the big
// model
// change
model.changedModel();
// end recording
ITextSelection selection = (ITextSelection) textEditor.getSelectionProvider().getSelection();
model.endRecording(this, selection.getOffset(), selection.getLength());
model.releaseFromEdit();
}
}
}
}
return null;
}
Aggregations