/** * Creates and returns an NLS tag edit for a string that is at the specified position in * a compilation unit. Returns <code>null</code> if the string is already NLSed * or the edit could not be created for some other reason. * @throws CoreException */ public static TextEdit createNLSEdit(ICompilationUnit cu, int position) throws CoreException { NLSLine nlsLine= scanCurrentLine(cu, position); if (nlsLine == null) return null; NLSElement element= findElement(nlsLine, position); if (element.hasTag()) return null; NLSElement[] elements= nlsLine.getElements(); int indexInElementList= Arrays.asList(elements).indexOf(element); int editOffset= computeInsertOffset(elements, indexInElementList, cu); String editText= ' ' + NLSElement.createTagText(indexInElementList + 1); //tags are 1-based return new InsertEdit(editOffset, editText); }
private static int computeInsertOffset(NLSElement[] elements, int index, ICompilationUnit cu) throws CoreException { NLSElement previousTagged= findPreviousTagged(index, elements); if (previousTagged != null) return previousTagged.getTagPosition().getOffset() + previousTagged.getTagPosition().getLength(); NLSElement nextTagged= findNextTagged(index, elements); if (nextTagged != null) return nextTagged.getTagPosition().getOffset(); return findLineEnd(cu, elements[index].getPosition().getOffset()); }
private static NLSElement findElement(NLSLine line, int position) { NLSElement[] elements= line.getElements(); for (int i= 0; i < elements.length; i++) { NLSElement element= elements[i]; if (isPositionInElement(element, position)) return element; } return null; }
if (!element.hasTag()) { for (int k= 0; k < positions.length; k++) { if (isPositionInElement(element, positions[k])) { int editOffset; if (j==0) { editOffset= elements[j+1].getTagPosition().getOffset(); } else { editOffset= findLineEnd(cu, element.getPosition().getOffset());
private static NLSLine scanCurrentLine(ICompilationUnit cu, int position) throws JavaModelException { try { Assert.isTrue(position >= 0 && position <= cu.getBuffer().getLength()); NLSLine[] allLines= NLSScanner.scan(cu); for (int i= 0; i < allLines.length; i++) { NLSLine line= allLines[i]; if (findElement(line, position) != null) return line; } return null; } catch (InvalidInputException e) { return null; } }
public String getCurrentContent() throws JavaModelException { IFile file= getOldFile(new NullProgressMonitor()); if (! file.exists()) return ""; //$NON-NLS-1$ InputStream stream= null; try{ stream= file.getContents(); String encoding= file.getCharset(); String c= NLSUtil.readString(stream, encoding); return (c == null) ? "": c; //$NON-NLS-1$ } catch (CoreException e){ throw new JavaModelException(e, IJavaModelStatusConstants.CORE_EXCEPTION); } finally { try { if (stream != null) stream.close(); } catch (IOException x) { } } }
public static StringFix createFix(CompilationUnit compilationUnit, IProblemLocation problem, boolean removeNLSTag, boolean addNLSTag) throws CoreException { TextEdit addEdit= null; ICompilationUnit cu= (ICompilationUnit)compilationUnit.getJavaElement(); if (addNLSTag) { addEdit= NLSUtil.createNLSEdit(cu, problem.getOffset()); } ReplaceEdit removeEdit= null; if (removeNLSTag) { IBuffer buffer= cu.getBuffer(); if (buffer != null) { removeEdit= getReplace(problem.getOffset(), problem.getLength(), buffer, true); } } if (addEdit != null && removeEdit != null) { String label= FixMessages.StringFix_AddRemoveNonNls_description; return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, addEdit), new TextEditGroup(label, removeEdit)}); } else if (addEdit != null) { String label= FixMessages.StringFix_AddNonNls_description; return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, addEdit)}); } else if (removeEdit != null) { String label= FixMessages.StringFix_RemoveNonNls_description; return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, removeEdit)}); } else { return null; } }
i++; TextEdit[] edits= NLSUtil.createNLSEdits(cu, positions); if (edits != null) { for (int j= 0; j < edits.length; j++) {
if (!element.hasTag()) { for (int k= 0; k < positions.length; k++) { if (isPositionInElement(element, positions[k])) { int editOffset; if (j==0) { editOffset= elements[j+1].getTagPosition().getOffset(); } else { editOffset= findLineEnd(cu, element.getPosition().getOffset());
private static NLSLine scanCurrentLine(ICompilationUnit cu, int position) throws JavaModelException { try { Assert.isTrue(position >= 0 && position <= cu.getBuffer().getLength()); NLSLine[] allLines= NLSScanner.scan(cu); for (int i= 0; i < allLines.length; i++) { NLSLine line= allLines[i]; if (findElement(line, position) != null) return line; } return null; } catch (InvalidInputException e) { return null; } catch (BadLocationException e) { return null; } }
public String getCurrentContent() throws JavaModelException { IFile file= getOldFile(new NullProgressMonitor()); if (! file.exists()) return ""; //$NON-NLS-1$ InputStream stream= null; try{ stream= file.getContents(); String encoding= file.getCharset(); String c= NLSUtil.readString(stream, encoding); return (c == null) ? "": c; //$NON-NLS-1$ } catch (CoreException e){ throw new JavaModelException(e, IJavaModelStatusConstants.CORE_EXCEPTION); } finally { try { if (stream != null) stream.close(); } catch (IOException x) { } } }
public static StringFix createFix(CompilationUnit compilationUnit, IProblemLocation problem, boolean removeNLSTag, boolean addNLSTag) throws CoreException { TextEdit addEdit= null; ICompilationUnit cu= (ICompilationUnit)compilationUnit.getJavaElement(); if (addNLSTag) { addEdit= NLSUtil.createNLSEdit(cu, problem.getOffset()); } ReplaceEdit removeEdit= null; if (removeNLSTag) { IBuffer buffer= cu.getBuffer(); if (buffer != null) { removeEdit= getReplace(problem.getOffset(), problem.getLength(), buffer, true); } } if (addEdit != null && removeEdit != null) { String label= FixMessages.StringFix_AddRemoveNonNls_description; return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, addEdit), new TextEditGroup(label, removeEdit)}); } else if (addEdit != null) { String label= FixMessages.StringFix_AddNonNls_description; return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, addEdit)}); } else if (removeEdit != null) { String label= FixMessages.StringFix_RemoveNonNls_description; return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, removeEdit)}); } else { return null; } }
i++; TextEdit[] edits= NLSUtil.createNLSEdits(cu, positions); if (edits != null) { for (int j= 0; j < edits.length; j++) {
private static int computeInsertOffset(NLSElement[] elements, int index, ICompilationUnit cu) throws CoreException { NLSElement previousTagged= findPreviousTagged(index, elements); if (previousTagged != null) return previousTagged.getTagPosition().getOffset() + previousTagged.getTagPosition().getLength(); NLSElement nextTagged= findNextTagged(index, elements); if (nextTagged != null) return nextTagged.getTagPosition().getOffset(); return findLineEnd(cu, elements[index].getPosition().getOffset()); }
/** * Creates and returns an NLS tag edit for a string that is at the specified position in a * compilation unit. * * @param cu the compilation unit * @param position position of the string * @return the edit, or <code>null</code> if the string is already NLSed or the edit could not * be created for some other reason. * @throws CoreException if scanning fails */ public static TextEdit createNLSEdit(ICompilationUnit cu, int position) throws CoreException { NLSLine nlsLine= scanCurrentLine(cu, position); if (nlsLine == null) return null; NLSElement element= findElement(nlsLine, position); if (element.hasTag()) return null; NLSElement[] elements= nlsLine.getElements(); int indexInElementList= Arrays.asList(elements).indexOf(element); int editOffset= computeInsertOffset(elements, indexInElementList, cu); String editText= ' ' + NLSElement.createTagText(indexInElementList + 1); //tags are 1-based return new InsertEdit(editOffset, editText); }
if (!element.hasTag()) { for (int k= 0; k < positions.length; k++) { if (isPositionInElement(element, positions[k])) { int editOffset; if (j==0) { editOffset= elements[j+1].getTagPosition().getOffset(); } else { editOffset= findLineEnd(cu, element.getPosition().getOffset());
private static NLSElement findElement(NLSLine line, int position) { NLSElement[] elements= line.getElements(); for (int i= 0; i < elements.length; i++) { NLSElement element= elements[i]; if (isPositionInElement(element, position)) return element; } return null; }
private static NLSLine scanCurrentLine(ICompilationUnit cu, int position) throws JavaModelException { try { Assert.isTrue(position >= 0 && position <= cu.getBuffer().getLength()); NLSLine[] allLines= NLSScanner.scan(cu); for (int i= 0; i < allLines.length; i++) { NLSLine line= allLines[i]; if (findElement(line, position) != null) return line; } return null; } catch (InvalidInputException e) { return null; } catch (BadLocationException e) { return null; } }
public String getCurrentContent() throws JavaModelException { IFile file= getOldFile(new NullProgressMonitor()); if (! file.exists()) return ""; //$NON-NLS-1$ InputStream stream= null; try{ stream= file.getContents(); String encoding= file.getCharset(); String c= NLSUtil.readString(stream, encoding); return (c == null) ? "": c; //$NON-NLS-1$ } catch (CoreException e){ throw new JavaModelException(e, IJavaModelStatusConstants.CORE_EXCEPTION); } finally { try { if (stream != null) stream.close(); } catch (IOException x) { } } }
public static StringFix createFix(CompilationUnit compilationUnit, IProblemLocation problem, boolean removeNLSTag, boolean addNLSTag) throws CoreException { TextEdit addEdit= null; ICompilationUnit cu= (ICompilationUnit)compilationUnit.getJavaElement(); if (addNLSTag) { addEdit= NLSUtil.createNLSEdit(cu, problem.getOffset()); } ReplaceEdit removeEdit= null; if (removeNLSTag) { IBuffer buffer= cu.getBuffer(); if (buffer != null) { removeEdit= getReplace(problem.getOffset(), problem.getLength(), buffer, true); } } if (addEdit != null && removeEdit != null) { String label= FixMessages.StringFix_AddRemoveNonNls_description; return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, addEdit), new TextEditGroup(label, removeEdit)}); } else if (addEdit != null) { String label= FixMessages.StringFix_AddNonNls_description; return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, addEdit)}); } else if (removeEdit != null) { String label= FixMessages.StringFix_RemoveNonNls_description; return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, removeEdit)}); } else { return null; } }