diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java index 920116132ee..f02524b4ede 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CAutoIndentStrategy.java @@ -566,18 +566,20 @@ public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy { firstLine= 0; } - // prefix: the part we need for formatting but won't paste - IRegion refLine= document.getLineInformationOfOffset(refOffset); - String prefix= document.get(refLine.getOffset(), newOffset - refLine.getOffset()); + // Prefix: the part we need for formatting but won't paste. + // Take up to 100 previous lines to preserve enough context. + int firstPrefixLine= Math.max(document.getLineOfOffset(refOffset) - 100, 0); + int prefixOffset= document.getLineInformation(firstPrefixLine).getOffset(); + String prefix= document.get(prefixOffset, newOffset - prefixOffset); - // handle the indentation computation inside a temporary document + // Handle the indentation computation inside a temporary document Document temp= new Document(prefix + newText); DocumentRewriteSession session= temp.startRewriteSession(DocumentRewriteSessionType.STRICTLY_SEQUENTIAL); scanner= new CHeuristicScanner(temp); indenter= new CIndenter(temp, scanner, fProject); installCPartitioner(temp); - // indent the first and second line + // Indent the first and second line // compute the relative indentation difference from the second line // (as the first might be partially selected) and use the value to // indent all other lines.