@@ -88,6 +88,17 @@ - (void)formatSelectedFiles {
88
88
}
89
89
90
90
- (void )formatDocument : (IDESourceCodeDocument *)document {
91
+ NSRect rect = [[TRVSXcodetextView ]visibleRect ];
92
+ NSPoint containerOrigin = [[TRVSXcodetextView ]textContainerOrigin ];
93
+ rect =NSOffsetRect (rect, -containerOrigin.x , -containerOrigin.y );
94
+ NSRange glyphRange = [[[TRVSXcodetextView ]layoutManager ]
95
+ glyphRangeForBoundingRect: rect
96
+ inTextContainer: [[TRVSXcodetextView ]textContainer ]];
97
+ NSRange charRange = [[[TRVSXcodetextView ]layoutManager ]
98
+ characterRangeForGlyphRange: glyphRange
99
+ actualGlyphRange: NULL ];
100
+
101
+
91
102
NSUInteger location = [[TRVSXcodetextView ]selectedRange ].location ;
92
103
NSUInteger length = [[documenttextStorage ]length ];
93
104
@@ -102,13 +113,15 @@ - (void)formatDocument:(IDESourceCodeDocument *)document {
102
113
103
114
if (documentIsLongerAfterFormatting && location > diff) {
104
115
location -= diff;
116
+ charRange.length -= diff;
105
117
}else if (!documentIsLongerAfterFormatting) {
106
118
location += diff;
119
+ charRange.length += diff;
107
120
}
108
121
109
122
NSRange range =NSMakeRange (location,0 );
110
123
[[TRVSXcodetextView ]setSelectedRange: range];
111
- [[TRVSXcodetextView ]scrollRangeToVisible: range ];
124
+ [[TRVSXcodetextView ]scrollRangeToVisible: charRange ];
112
125
}
113
126
114
127
#pragma mark - Private