@Override public ITypedRegion getPartition(int offset) { try { Position[] category = fDocument.getPositions(fPositionCategory); if (category == null || category.length == 0) return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE); int index= fDocument.computeIndexInCategory(fPositionCategory, offset); if (index < category.length) { TypedPosition next= (TypedPosition) category[index]; if (offset == next.offset) return new TypedRegion(next.getOffset(), next.getLength(), next.getType()); if (index == 0) return new TypedRegion(0, next.offset, IDocument.DEFAULT_CONTENT_TYPE); TypedPosition previous= (TypedPosition) category[index - 1]; if (previous.includes(offset)) return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType()); int endOffset= previous.getOffset() + previous.getLength(); return new TypedRegion(endOffset, next.getOffset() - endOffset, IDocument.DEFAULT_CONTENT_TYPE); } TypedPosition previous= (TypedPosition) category[category.length - 1]; if (previous.includes(offset)) return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType()); int endOffset= previous.getOffset() + previous.getLength(); return new TypedRegion(endOffset, fDocument.getLength() - endOffset, IDocument.DEFAULT_CONTENT_TYPE); } catch (BadPositionCategoryException x) { } catch (BadLocationException x) { } return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE); }
gapOffset= (previous != null) ? previous.getOffset() + previous.getLength() : 0; gap= new Position(gapOffset, current.getOffset() - gapOffset); if ((includeZeroLengthPartitions || gap.getLength() > 0) && gap.overlapsWith(offset, length)) { start= Math.max(offset, gapOffset); if (current.overlapsWith(offset, length)) { start= Math.max(offset, current.getOffset()); end= Math.min(endOffset, current.getOffset() + current.getLength()); list.add(new TypedRegion(start, end - start, current.getType())); gapOffset= previous.getOffset() + previous.getLength(); gap= new Position(gapOffset, fDocument.getLength() - gapOffset); if ((includeZeroLengthPartitions || gap.getLength() > 0) && ((includeZeroLengthPartitions && offset + length == gapOffset && gap.length == 0) || gap.overlapsWith(offset, length))) {
private void removeRangeMarkers(List<TypedPosition> positions, IDocument document, TemplateVariable[] variables) throws MalformedTreeException, BadLocationException, BadPositionCategoryException { // revert previous changes for (Iterator<TypedPosition> it= positions.iterator(); it.hasNext();) { TypedPosition position= it.next(); // remove and re-add in order to not confuse ExclusivePositionUpdater document.removePosition(CATEGORY, position); final String original= position.getType(); if (original != null) { document.replace(position.getOffset(), position.getLength(), original); position.setLength(original.length()); } document.addPosition(position); } Iterator<TypedPosition> it= positions.iterator(); for (int i= 0; i != variables.length; i++) { TemplateVariable variable= variables[i]; int[] offsets= new int[variable.getOffsets().length]; for (int j= 0; j != offsets.length; j++) offsets[j]= it.next().getOffset(); variable.setOffsets(offsets); } } }
@Override public void format() { super.format(); final IDocument document = fDocuments.removeFirst(); final TypedPosition partition = fPartitions.removeFirst(); if (document == null || partition == null) { return; } try { String formatted = formatElement(document, partition); String partitionText = document.get(partition.getOffset(), partition.getLength()); if (formatted != null && !formatted.equals(partitionText)) { document.replace(partition.getOffset(), partition.getLength(), formatted); } } catch (BadLocationException e) { // do nothing } }
if (first > 0) { TypedPosition partition= (TypedPosition) category[first - 1]; if (partition.includes(reparseStart)) { partitionStart= partition.getOffset(); contentType= partition.getType(); if (e.getOffset() == partition.getOffset() + partition.getLength()) reparseStart= partitionStart; -- first; } else if (reparseStart == e.getOffset() && reparseStart == partition.getOffset() + partition.getLength()) { partitionStart= partition.getOffset(); contentType= partition.getType(); reparseStart= partitionStart; -- first; } else { partitionStart= partition.getOffset() + partition.getLength(); contentType= IDocument.DEFAULT_CONTENT_TYPE; TypedPosition p= (TypedPosition) category[first]; if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!d.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) { d.addPosition(fPositionCategory, new TypedPosition(start, length, contentType)); rememberRegion(start, length); } catch (BadPositionCategoryException x) {
int sourceOffset= document.getLineOffset(document.getLineOfOffset(position.getOffset())); int partitionOffset= position.getOffset() - sourceOffset; int sourceLength= partitionOffset + position.getLength(); String source= document.get(sourceOffset, sourceLength); CodeFormatter commentFormatter= ToolFactory.createCodeFormatter(preferences, ToolFactory.M_FORMAT_EXISTING); int indentationLevel= inferIndentationLevel(source.substring(0, partitionOffset), getTabSize(preferences), getIndentSize(preferences)); edit= commentFormatter.format(getKindForPartitionType(position.getType()), source, partitionOffset, position.getLength(), indentationLevel, TextUtilities.getDefaultLineDelimiter(document)); int sourceOffset= document.getLineOffset(document.getLineOfOffset(position.getOffset())); int partitionOffset= position.getOffset() - sourceOffset; int sourceLength= partitionOffset + position.getLength(); String source= document.get(sourceOffset, sourceLength); CodeFormatter commentFormatter= ToolFactory.createCodeFormatter(preferences); int indentationLevel= inferIndentationLevel(source.substring(0, partitionOffset), getTabSize(preferences), getIndentSize(preferences)); edit= commentFormatter.format(getKindForPartitionType(position.getType()), source, partitionOffset, position.getLength(), indentationLevel, TextUtilities.getDefaultLineDelimiter(document));
/** * Formats the document specified in the formatting context with the * formatting strategy registered for the content type. * <p> * For this formatting type only slave strategies are used. The region to be * formatted is aligned on partition boundaries of the underlying content * type. The exact formatting strategy is determined by the underlying * content type of the document partitioning. * * @param context The formatting context to use * @param document The document to operate on * @param offset The offset of the region to format * @param length The length of the region to format * @param type The content type of the region to format */ protected void formatSlave(final IFormattingContext context, final IDocument document, final int offset, final int length, final String type) { final IFormattingStrategyExtension strategy= (IFormattingStrategyExtension)fSlaves.get(type); if (strategy != null) { context.setProperty(FormattingContextProperties.CONTEXT_PARTITION, new TypedPosition(offset, length, type)); strategy.formatterStarts(context); strategy.format(); strategy.formatterStops(); } }
TypedPosition p= (TypedPosition) category[first]; if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!d.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) { d.addPosition(fPositionCategory, new TypedPosition(start, length, contentType)); rememberRegion(start, length); } catch (BadPositionCategoryException x) {
if (first > 0) { TypedPosition partition= (TypedPosition) category[first - 1]; if (partition.includes(reparseStart)) { partitionStart= partition.getOffset(); contentType= partition.getType(); if (e.getOffset() == partition.getOffset() + partition.getLength()) reparseStart= partitionStart; -- first; } else if (reparseStart == e.getOffset() && reparseStart == partition.getOffset() + partition.getLength()) { partitionStart= partition.getOffset(); contentType= partition.getType(); reparseStart= partitionStart; -- first; } else { partitionStart= partition.getOffset() + partition.getLength(); contentType= IDocument.DEFAULT_CONTENT_TYPE; TypedPosition p= (TypedPosition) category[first]; if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!d.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) { d.addPosition(fPositionCategory, new TypedPosition(start, length, contentType)); rememberRegion(start, length); } catch (BadPositionCategoryException x) {
private String formatElement(IDocument document, TypedPosition partition) throws BadLocationException { String partitionText = document.get(partition.getOffset(), partition.getLength()); IRegion line = document.getLineInformationOfOffset(partition.getOffset()); int indentLength = partition.getOffset() - line.getOffset(); String lineDelimiter = document.getLineDelimiter(document.getLineOfOffset(line.getOffset())); if (lineDelimiter == null) { lineDelimiter = TextUtilities.getDefaultLineDelimiter(document); } return XmlTagFormatter.format(partitionText, prefs, document.get(line.getOffset(), indentLength), lineDelimiter); }
/** * Formats the document specified in the formatting context with the * formatting strategy registered for the content type. * <p> * For this formatting type only slave strategies are used. The region to be * formatted is aligned on partition boundaries of the underlying content * type. The exact formatting strategy is determined by the underlying * content type of the document partitioning. * * @param context The formatting context to use * @param document The document to operate on * @param offset The offset of the region to format * @param length The length of the region to format * @param type The content type of the region to format */ protected void formatSlave(final IFormattingContext context, final IDocument document, final int offset, final int length, final String type) { final IFormattingStrategyExtension strategy= (IFormattingStrategyExtension)fSlaves.get(type); if (strategy != null) { context.setProperty(FormattingContextProperties.CONTEXT_PARTITION, new TypedPosition(offset, length, type)); strategy.formatterStarts(context); strategy.format(); strategy.formatterStops(); } }
TypedPosition p= (TypedPosition) category[first]; if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!d.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) { d.addPosition(fPositionCategory, new TypedPosition(start, length, contentType)); rememberRegion(start, length); } catch (BadPositionCategoryException x) {
if (first > 0) { TypedPosition partition= (TypedPosition) category[first - 1]; if (partition.includes(reparseStart)) { partitionStart= partition.getOffset(); contentType= partition.getType(); if (e.getOffset() == partition.getOffset() + partition.getLength()) reparseStart= partitionStart; -- first; } else if (reparseStart == e.getOffset() && reparseStart == partition.getOffset() + partition.getLength()) { partitionStart= partition.getOffset(); contentType= partition.getType(); reparseStart= partitionStart; -- first; } else { partitionStart= partition.getOffset() + partition.getLength(); contentType= IDocument.DEFAULT_CONTENT_TYPE; TypedPosition p= (TypedPosition) category[first]; if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!fDocument.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) { fDocument.addPosition(fPositionCategory, new TypedPosition(start, length, contentType)); rememberRegion(start, length); } catch (BadPositionCategoryException x) {
@Override public ITypedRegion getPartition(int offset) { try { Position[] category = fDocument.getPositions(fPositionCategory); if (category == null || category.length == 0) return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE); int index= fDocument.computeIndexInCategory(fPositionCategory, offset); if (index < category.length) { TypedPosition next= (TypedPosition) category[index]; if (offset == next.offset) return new TypedRegion(next.getOffset(), next.getLength(), next.getType()); if (index == 0) return new TypedRegion(0, next.offset, IDocument.DEFAULT_CONTENT_TYPE); TypedPosition previous= (TypedPosition) category[index - 1]; if (previous.includes(offset)) return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType()); int endOffset= previous.getOffset() + previous.getLength(); return new TypedRegion(endOffset, next.getOffset() - endOffset, IDocument.DEFAULT_CONTENT_TYPE); } TypedPosition previous= (TypedPosition) category[category.length - 1]; if (previous.includes(offset)) return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType()); int endOffset= previous.getOffset() + previous.getLength(); return new TypedRegion(endOffset, fDocument.getLength() - endOffset, IDocument.DEFAULT_CONTENT_TYPE); } catch (BadPositionCategoryException x) { } catch (BadLocationException x) { } return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE); }
gapOffset= (previous != null) ? previous.getOffset() + previous.getLength() : 0; gap= new Position(gapOffset, current.getOffset() - gapOffset); if ((includeZeroLengthPartitions || gap.getLength() > 0) && gap.overlapsWith(offset, length)) { start= Math.max(offset, gapOffset); if (current.overlapsWith(offset, length)) { start= Math.max(offset, current.getOffset()); end= Math.min(endOffset, current.getOffset() + current.getLength()); list.add(new TypedRegion(start, end - start, current.getType())); gapOffset= previous.getOffset() + previous.getLength(); gap= new Position(gapOffset, fDocument.getLength() - gapOffset); if ((includeZeroLengthPartitions || gap.getLength() > 0) && ((includeZeroLengthPartitions && offset + length == gapOffset && gap.length == 0) || gap.overlapsWith(offset, length))) {
@Override public void format() { super.format(); final IDocument document= fDocuments.removeFirst(); final TypedPosition partition= fPartitions.removeFirst(); final String path= fPaths.removeFirst(); if (document != null && partition != null) { Map<String, IDocumentPartitioner> partitioners= null; try { final boolean isModuleInfo= path != null && path.endsWith(JavaModelUtil.MODULE_INFO_JAVA); final int kind= (isModuleInfo ? CodeFormatter.K_MODULE_INFO : CodeFormatter.K_COMPILATION_UNIT) | CodeFormatter.F_INCLUDE_COMMENTS; final TextEdit edit= CodeFormatterUtil.reformat(kind, document.get(), partition.getOffset(), partition.getLength(), 0, TextUtilities.getDefaultLineDelimiter(document), getPreferences()); if (edit != null) { if (edit.getChildrenSize() > 20) partitioners= TextUtilities.removeDocumentPartitioners(document); edit.apply(document); } } catch (MalformedTreeException exception) { JavaPlugin.log(exception); } catch (BadLocationException exception) { // Can only happen on concurrent document modification - log and bail out JavaPlugin.log(exception); } finally { if (partitioners != null) TextUtilities.addDocumentPartitioners(document, partitioners); } } }