/** * {@inheritDoc} * * @see java.util.concurrent.Callable#call() */ public CompilationResult call() throws Exception { checkCancelled(); String fullExpression = acceleoSource.rebuildFullExpression(context); ResourceSet resourceSet = new ResourceSetImpl(); Resource resource = ModelUtils.createResource( URI.createURI("http://acceleo.eclipse.org/default.emtl"), resourceSet); // $NON-NLS-1$ AcceleoSourceBuffer source = new AcceleoSourceBuffer(new StringBuffer(fullExpression)); List<URI> dependencies = new ArrayList<URI>(); if (acceleoSource.getModuleImport() != null) { dependencies.addAll(computeImportList(acceleoSource.getModuleImport(), resourceSet)); } AcceleoParser parser = new AcceleoParser(); parser.parse(source, resource, dependencies); checkCancelled(); ASTNode selectedNode = null; if (!resource.getContents().isEmpty()) { Module module = (Module) resource.getContents().get(0); ISelection selection = context.getSelection(); if (selection instanceof ITextSelection && ((ITextSelection) selection).getLength() > 0) { ITextSelection textSelection = (ITextSelection) selection; int startOffset = textSelection.getOffset() + acceleoSource.getGap(); int endOffset = startOffset + textSelection.getLength(); if (textSelection.getText().startsWith("[")) { // $NON-NLS-1$ startOffset++; } if (textSelection.getText().endsWith("/]")) { // $NON-NLS-1$ endOffset -= 2; } selectedNode = getChildrenCandidate(module, startOffset, endOffset); if (textSelection.getLength() == 0) { while (selectedNode != null && !(selectedNode instanceof ModuleElement)) { selectedNode = (ASTNode) selectedNode.eContainer(); } } } if (selectedNode == null && module != null && !module.getOwnedModuleElement().isEmpty()) { selectedNode = module.getOwnedModuleElement().get(0); } } checkCancelled(); IStatus problems = parseProblems(source.getProblems(), source.getWarnings(), source.getInfos()); return new CompilationResult(selectedNode, problems); }
public String normalizeSourceSelection(ITextSelection selection) { String selectedText = ""; if (selection.getText() != null) { selectedText = selection.getText().trim(); } if (selectedText.length() == 0) { return ""; } try { return normalizeBlockIndentation(selection, selectedText); } catch (Throwable e) { /* TODO: uncommented empty exception catch all */ } return selectedText; }
/** * the command has been executed, so extract extract the needed information from the application * context. */ public Object execute(ExecutionEvent event) throws ExecutionException { IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); ITextSelection content = (ITextSelection) window .getActivePage() .getActiveEditor() .getEditorSite() .getSelectionProvider() .getSelection(); String texto = content.getText(); Toolkit toolkit = Toolkit.getDefaultToolkit(); Clipboard clipboard = toolkit.getSystemClipboard(); String sqlFormatado = formatSQL(texto); StringSelection textoFormatado = new StringSelection(sqlFormatado); clipboard.setContents(textoFormatado, null); if (isBlank(texto)) { MessageDialog.openInformation(window.getShell(), "SQLCopy", "Você não selecionou nada!"); } else { if (isConvertToJava( texto)) { // Realizar a substituição do texto selecionado apenas quando estiver // convertendo um SQL para Java. IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); ITextEditor editor = (ITextEditor) part; IDocumentProvider prov = editor.getDocumentProvider(); IDocument doc = prov.getDocument(editor.getEditorInput()); ISelection sel = editor.getSelectionProvider().getSelection(); if (sel instanceof TextSelection) { final TextSelection textSel = (TextSelection) sel; try { // IRegion region = doc.getLineInformationOfOffset(textSel.getOffset()); // doc.replace( textSel.getOffset(), textSel.getLength(), // identingCode(region.getLength(), sqlFormatado)); doc.replace(textSel.getOffset(), textSel.getLength(), sqlFormatado); } catch (BadLocationException e) { e.printStackTrace(); } } } // MessageDialog.openInformation( // window.getShell(), // "SQLCopy", // sqlFormatado.toString()); CustomMessageDialog dialog = new CustomMessageDialog(window.getShell(), sqlFormatado.toString()); dialog.open(); } return null; }
private boolean isNonTrivialSelection(ITextSelection selection) { if (selection.getLength() < 30) { String text = selection.getText(); if (text != null) { for (int i = 0; i < text.length(); i++) { if (!Character.isJavaIdentifierPart(text.charAt(i))) { return true; } } } return false; } return true; }
/** * Returns the C model element at the given selection. * * @param part Workbench part where the selection is. * @param selection Selection in part. * @return C model element if found. */ protected ICElement getCElementFromSelection(IWorkbenchPart part, ISelection selection) { if (selection instanceof ITextSelection) { ITextSelection textSelection = (ITextSelection) selection; String text = textSelection.getText(); if (text != null) { if (part instanceof ITextEditor) { ICElement editorElement = CDTUITools.getEditorInputCElement(((ITextEditor) part).getEditorInput()); if (editorElement instanceof ITranslationUnit) { ITranslationUnit tu = (ITranslationUnit) editorElement; try { if (tu.isStructureKnown() && tu.isConsistent()) { return tu.getElementAtOffset(textSelection.getOffset()); } } catch (CModelException exc) { // ignored on purpose } } } else { IResource resource = getResource(part); if (resource instanceof IFile) { ITranslationUnit tu = getTranslationUnit((IFile) resource); if (tu != null) { try { ICElement element = tu.getElement(text.trim()); if (element == null) { element = tu.getElementAtLine(textSelection.getStartLine()); } return element; } catch (CModelException e) { } } } } } } else if (selection instanceof IStructuredSelection) { IStructuredSelection ss = (IStructuredSelection) selection; if (ss.size() == 1) { Object object = ss.getFirstElement(); if (object instanceof ICElement) { return (ICElement) object; } } } return null; }
private SearchPatternData trySimpleTextSelection(final ITextSelection selection) { final String selectedText = selection.getText(); if (selectedText != null && selectedText.length() > 0) { int i = 0; while (i < selectedText.length() && !SearchCoreUtil.isLineDelimiterChar(selectedText.charAt(i))) { i++; } if (i > 0) { final String s = selectedText.substring(0, i); // FIXME find the module from the editor somehow return new SearchPatternData( s, getContainer().getSelectedScope(), getLimitTo(), getSearchFor(), getContainer().getSelectedWorkingSets(), getLastIncludeMask()); } } return null; }
/** Override to improve matching accuracy */ @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]; } Region selectionRegion = new Region(selection.getOffset(), selection.getLength()); TemplateContext selectionContext = createContext(viewer, selectionRegion); int lineOffset = 0; try { IRegion lineInformationOfOffset = viewer.getDocument().getLineInformationOfOffset(offset); lineOffset = offset - lineInformationOfOffset.getOffset(); } catch (BadLocationException e1) { // ignore } String selectionText = selection.getText(); context.setVariable("selection", selectionText); // $NON-NLS-1$ selectionContext.setVariable("selection", selectionText); // $NON-NLS-1$ context.setVariable("text", selectionText); // $NON-NLS-1$ selectionContext.setVariable("text", selectionText); // $NON-NLS-1$ Template[] templates = getTemplates(context.getContextType().getId()); List<ICompletionProposal> matches = new ArrayList<>(templates.length); for (Template template : templates) { try { context.getContextType().validate(template.getPattern()); } catch (TemplateException e) { continue; } if (!template.matches(prefix, context.getContextType().getId())) { continue; } boolean selectionBasedMatch = isSelectionBasedMatch(template, context); if (template.getName().startsWith(prefix) || selectionBasedMatch) { int relevance = getRelevance(template, lineOffset, prefix); if (selectionBasedMatch) { matches.add( createProposal(template, selectionContext, (IRegion) selectionRegion, relevance)); } else { matches.add(createProposal(template, context, (IRegion) region, relevance)); } } } Collections.sort(matches, proposalComparator); return matches.toArray(new ICompletionProposal[matches.size()]); }