public String apply(String contents) { final TextEdit edit = codeFormatter.format( CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, contents, 0, contents.length(), 0, Constants.LINE_SEPARATOR); if (edit == null) { // TODO log a fatal or warning here. Throwing an exception is causing the actual freemarker error to be lost return contents; } IDocument document = new Document(contents); try { edit.apply(document); } catch (Exception e) { throw new RuntimeException( "Failed to format the generated source code.", e); } return document.get(); } }
private static int getNextPosition(IDocument doc, int offset, char ch) { int i = 0; try { for (i = 0; i + offset < doc.getLength(); i++) { if (ch == doc.get(offset + i, 1).toCharArray()[0]) break; } } catch (BadLocationException e) { } return i; }
private String getPrefix(ITextViewer viewer, int offset) throws BadLocationException { IDocument doc= viewer.getDocument(); if (doc == null || offset > doc.getLength()) return null; int length= 0; while (--offset >= 0 && Character.isJavaIdentifierPart(doc.getChar(offset))) length++; return doc.get(offset + 1, length); }
private int computeCharacterColumn(int line, int visualColumn) throws BadLocationException { IRegion info= fDocument.getLineInformation(line); int lineLength= info.getLength(); String content= fDocument.get(info.getOffset(), lineLength); int visual= 0; for (int offset= 0; offset < lineLength; offset++) { if (visual >= visualColumn) return offset; visual+= visualSizeIncrement(content.charAt(offset), visual); } return lineLength + Math.max(0, visualColumn - visual); }
private static String getIndentAt(IDocument document, int offset, CodeGenerationSettings settings) { try { IRegion region= document.getLineInformationOfOffset(offset); return IndentManipulation.extractIndentString(document.get(region.getOffset(), region.getLength()), settings.tabWidth, settings.indentWidth); } catch (BadLocationException e) { return ""; //$NON-NLS-1$ } }
@Override public CharSequence subSequence(int start, int end) { try { return fDocument.get(start, end - start); } catch (BadLocationException e) { throw new IndexOutOfBoundsException(); } }
public boolean validate(IDocument document, int offset, DocumentEvent event) { try { String content= document.get(fReplacementOffset, fReplacementLength); if (content.startsWith(fReplacementString)) return true; } catch (BadLocationException e) { // ignore concurrently modified document } return false; } }
public String getText(int offset, int length) { try { return fDocument.get(offset, length); } catch (BadLocationException x) { throw new ArrayIndexOutOfBoundsException(); } }
private boolean isPrefixCompatible(CharSequence oneSequence, int oneOffset, CharSequence twoSequence, int twoOffset, IDocument document, boolean ignoreCase) throws BadLocationException { if (oneSequence == null || twoSequence == null) return true; int min= Math.min(oneOffset, twoOffset); int oneEnd= oneOffset + oneSequence.length(); int twoEnd= twoOffset + twoSequence.length(); String one= document.get(oneOffset, min - oneOffset) + oneSequence + document.get(oneEnd, Math.min(fFilterOffset, fFilterOffset - oneEnd)); String two= document.get(twoOffset, min - twoOffset) + twoSequence + document.get(twoEnd, Math.min(fFilterOffset, fFilterOffset - twoEnd)); return ignoreCase ? one.equalsIgnoreCase(two) : one.equals(two); }
@Override public boolean validate(IDocument document, int offset, DocumentEvent event) { try { int prefixStart= fOffset - fPrefix.length(); return offset >= fOffset && offset < fOffset + fString.length() && document.get(prefixStart, offset - (prefixStart)).equals((fPrefix + fString).substring(0, offset - prefixStart)); } catch (BadLocationException x) { return false; } }
protected int[] getLineAndColumn(IDocument document, HighlightedPositionCore position) { //@formatter:off int[] lineAndColumn = JsonRpcHelpers.toLine(document, position.offset); Assert.isNotNull( lineAndColumn, "Cannot retrieve the line and column information for document. Position was: " + position + " Document was:>" + document.get() + "<." ); return lineAndColumn; //@formatter:off }
/** * @return The text of the editor's document, or null if unavailable. */ public String getText() { IDocument doc = getDocument(); return doc==null? null : doc.get(); }
public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubject, int documentOffset) { if (fRoot == null) { return null; } String input= contentAssistSubject.getDocument().get(); String prefix= input.substring(0, documentOffset); ICompletionProposal[] proposals= createSourcePackageFragmentRootProposals(prefix, input.length()); Arrays.sort(proposals, fComparator); return proposals; }
public String getCurrentEditorContent() { final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() .getActiveEditor(); if (!(editor instanceof ITextEditor)) return null; ITextEditor ite = (ITextEditor)editor; IDocument doc = ite.getDocumentProvider().getDocument(ite.getEditorInput()); return doc.get(); }
@Override int performDocumentUpdating(IDocument document) throws BadLocationException { fText= document.get(getOffset(), getLength()); fDelta= 0; return fDelta; }
public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { if (fCompletionContextRequestor.getOriginalCu() == null) return null; String input= contentAssistSubjectControl.getDocument().get(); if (documentOffset == 0) return null; ICompletionProposal[] proposals= internalComputeCompletionProposals(documentOffset, input); Arrays.sort(proposals, fComparator); return proposals; }
protected void okPressed() { String name= fNameText == null ? fOriginalTemplate.getName() : fNameText.getText(); boolean isAutoInsertable= fAutoInsertCheckbox != null && fAutoInsertCheckbox.getSelection(); fNewTemplate= new Template(name, fDescriptionText.getText(), getContextId(), fPatternEditor.getDocument().get(), isAutoInsertable); super.okPressed(); }
public String getHoverInfo(ITextViewer textViewer, IRegion region) { try { IDocument doc = textViewer.getDocument(); String text = doc.get(region.getOffset(), region.getLength()); return "<b>"+text+"</b>"; } catch (Exception e) { return null; } }
@Override public void documentAboutToBeChanged(DocumentEvent event) { try { fReplacedText= event.getDocument().get(event.getOffset(), event.getLength()); fPreservedUndoModificationStamp= event.getModificationStamp(); } catch (BadLocationException x) { fReplacedText= null; } }
public void documentChanged(DocumentEvent event) { try { fRefactoring.setBody(event.getDocument().get(), fRefactoring.getMethod().getParameterNames()); } catch (JavaModelException ex) { // TODO Auto-generated catch block JavaPlugin.log(ex); } } });