Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 77053a62e74e2ca546ad86a9a9eb4659e45fd548 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
/*******************************************************************************
 * Copyright (c) 2000, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     QNX Software System
 *     Anton Leherbauer (Wind River Systems)
 *     Sergey Prigogin (Google)
 *     Andrew Ferguson (Symbian)
 *     Andrew Gvozdev
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.text;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.DocumentCommand;
import org.eclipse.jface.text.DocumentRewriteSession;
import org.eclipse.jface.text.DocumentRewriteSessionType;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.texteditor.ITextEditorExtension3;

import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.ui.text.ICPartitions;

import org.eclipse.cdt.internal.corext.util.CodeFormatterUtil;

import org.eclipse.cdt.internal.ui.editor.IndentUtil;
import org.eclipse.cdt.internal.ui.text.CIndenter.MatchMode;

/**
 * Auto indent strategy sensitive to brackets.
 */
public class CAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
	/** The line comment introducer. Value is "{@value}" */
	private static final String LINE_COMMENT= "//"; //$NON-NLS-1$
//	private static final GCCScannerExtensionConfiguration C_GNU_SCANNER_EXTENSION = new GCCScannerExtensionConfiguration();

//	private static class CompilationUnitInfo {
//		char[] buffer;
//		int delta;
//
//		CompilationUnitInfo(char[] buffer, int delta) {
//			this.buffer = buffer;
//			this.delta = delta;
//		}
//	}

	private boolean fCloseBrace;
	private boolean fIsSmartMode;

	private String fPartitioning;
	private final ICProject fProject;

	/**
	 * Creates a new C auto indent strategy for the given document partitioning.
	 *
	 * @param partitioning the document partitioning
	 * @param project the project to get formatting preferences from, or null to use default preferences
	 */
	public CAutoIndentStrategy(String partitioning, ICProject project) {
		fPartitioning = partitioning;
		fProject = project;
 	}

	private int getBracketCount(IDocument d, int start, int end, boolean ignoreCloseBrackets) throws BadLocationException {
		int bracketcount = 0;
		while (start < end) {
			char curr = d.getChar(start);
			start++;
			switch (curr) {
				case '/' :
					if (start < end) {
						char next = d.getChar(start);
						if (next == '*') {
							// a comment starts, advance to the comment end
							start = getCommentEnd(d, start + 1, end);
						} else if (next == '/') {
							// '//'-comment: nothing to do anymore on this line
							start = end;
						}
					}
					break;
				case '*' :
					if (start < end) {
						char next = d.getChar(start);
						if (next == '/') {
							// we have been in a comment: forget what we read before
							bracketcount = 0;
							start++;
						}
					}
					break;
				case '{' :
					bracketcount++;
					ignoreCloseBrackets = false;
					break;
				case '}' :
					if (!ignoreCloseBrackets) {
						bracketcount--;
					}
					break;
				case '"' :
				case '\'' :
					start = getStringEnd(d, start, end, curr);
					break;
				default :
			}
		}
		return bracketcount;
	}

	// ----------- bracket counting ------------------------------------------------------

	private int getCommentEnd(IDocument d, int pos, int end) throws BadLocationException {
		while (pos < end) {
			char curr = d.getChar(pos);
			pos++;
			if (curr == '*') {
				if (pos < end && d.getChar(pos) == '/') {
					return pos + 1;
				}
			}
		}
		return end;
	}

	private String getIndentOfLine(IDocument d, int line) throws BadLocationException {
		if (line > -1) {
			int start = d.getLineOffset(line);
			int end = start + d.getLineLength(line) - 1;
			int whiteend = findEndOfWhiteSpace(d, start, end);
			return d.get(start, whiteend - start);
		}
		return ""; //$NON-NLS-1$
	}

	private int getStringEnd(IDocument d, int pos, int end, char ch) throws BadLocationException {
		while (pos < end) {
			char curr = d.getChar(pos);
			pos++;
			if (curr == '\\') {
				// ignore escaped characters
				pos++;
			} else if (curr == ch) {
				return pos;
			}
		}
		return end;
	}

	private void smartIndentAfterClosingBracket(IDocument d, DocumentCommand c) {
		if (c.offset == -1 || d.getLength() == 0)
			return;

		try {
			int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset);
			int line = d.getLineOfOffset(p);
			int start = d.getLineOffset(line);
			int whiteend = findEndOfWhiteSpace(d, start, c.offset);

			CHeuristicScanner scanner= new CHeuristicScanner(d);
			ITypedRegion partition= TextUtilities.getPartition(d, fPartitioning, p, false);
			if (ICPartitions.C_PREPROCESSOR.equals(partition.getType())) {
				scanner = new CHeuristicScanner(d, fPartitioning, ICPartitions.C_PREPROCESSOR);
			}
			CIndenter indenter = new CIndenter(d, scanner, fProject);

			// shift only when line does not contain any text up to the closing bracket
			if (whiteend == c.offset) {
				// evaluate the line with the opening bracket that matches out closing bracket
				int reference = indenter.findReferencePosition(c.offset, false, MatchMode.MATCH_BRACE);
				int indLine = d.getLineOfOffset(reference);
				if (indLine != -1 && indLine != line) {
					// take the indent of the found line
					StringBuilder replaceText = new StringBuilder(getIndentOfLine(d, indLine));
					// add the rest of the current line including the just added close bracket
					replaceText.append(d.get(whiteend, c.offset - whiteend));
					replaceText.append(c.text);
					// modify document command
					c.length += c.offset - start;
					c.offset = start;
					c.text = replaceText.toString();
				}
			}
		} catch (BadLocationException e) {
			CUIPlugin.log(e);
		}
	}

	private void smartIndentAfterOpeningBracket(IDocument d, DocumentCommand c) {
		if (c.offset < 1 || d.getLength() == 0)
			return;

		int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset);

		try {
			CHeuristicScanner scanner= new CHeuristicScanner(d);
			ITypedRegion partition= TextUtilities.getPartition(d, fPartitioning, p, false);
			if (ICPartitions.C_PREPROCESSOR.equals(partition.getType())) {
				scanner = new CHeuristicScanner(d, fPartitioning, ICPartitions.C_PREPROCESSOR);
			}
			// current line
			int line = d.getLineOfOffset(c.offset);
			int lineOffset = d.getLineOffset(line);

			// make sure we don't have any leading comments etc.
			if (!d.get(lineOffset, c.offset - lineOffset).trim().isEmpty())
				return;

			// Line of last C code
			int pos = scanner.findNonWhitespaceBackward(p, CHeuristicScanner.UNBOUND);
			if (pos == -1)
				return;
			int lastLine = d.getLineOfOffset(pos);

			// Only shift if the last C line is further up and is a braceless block candidate
			if (lastLine < line) {
				CIndenter indenter = new CIndenter(d, scanner, fProject);
				StringBuilder indent = indenter.computeIndentation(p, true);
				String toDelete = d.get(lineOffset, c.offset - lineOffset);
				if (indent != null && !indent.toString().equals(toDelete)) {
					c.text = indent.append(c.text).toString();
					c.length += c.offset - lineOffset;
					c.offset = lineOffset;
				}
			}
		} catch (BadLocationException e) {
			CUIPlugin.log(e);
		}
	}

	private void smartIndentAfterNewLine(IDocument d, DocumentCommand c) {
		int docLength = d.getLength();
		if (c.offset == -1 || docLength == 0)
			return;

		int addIndent= 0;
		CHeuristicScanner scanner= new CHeuristicScanner(d);
		try {
			ITypedRegion partition= TextUtilities.getPartition(d, fPartitioning, c.offset, false);
			if (ICPartitions.C_PREPROCESSOR.equals(partition.getType()) && c.offset > 0 && d.getChar(c.offset-1) == '\\') {
				scanner = new CHeuristicScanner(d, fPartitioning, ICPartitions.C_PREPROCESSOR);
				addIndent= 1;
			}

			int line = d.getLineOfOffset(c.offset);
			IRegion reg = d.getLineInformation(line);
			int start = reg.getOffset();
			int lineEnd = start + reg.getLength();

			StringBuilder indent= null;
			CIndenter indenter= new CIndenter(d, scanner, fProject);
			if (getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_AUTO_INDENT)) {
				indent= indenter.computeIndentation(c.offset);
			} else {
				// reuse existing indent
				int wsEnd= findEndOfWhiteSpace(d, start, c.offset);
				if (wsEnd > start) {
					indent= new StringBuilder(d.get(start, wsEnd - start));
					addIndent= 0;
				}
			}
			if (indent == null) {
				indent= new StringBuilder();
			}
			if (addIndent > 0 && indent.length() == 0) {
				indent= indenter.createReusingIndent(indent, addIndent, 0);
			}

			StringBuilder buf = new StringBuilder(c.text + indent);
			int contentStart = findEndOfWhiteSpace(d, c.offset, lineEnd);
			c.length =  Math.max(contentStart - c.offset, 0);

			// insert closing brace on new line after an unclosed opening brace
			if (getBracketCount(d, start, c.offset, true) > 0 && fCloseBrace && !isClosedBrace(d, c.offset, c.length)) {
				c.caretOffset = c.offset + buf.length();
				c.shiftsCaret = false;

				// copy old content of line behind insertion point to new line
				// unless we think we are inserting an anonymous type definition
				if (c.offset == 0 || !(computeAnonymousPosition(d, c.offset - 1, fPartitioning, lineEnd) != -1)) {
					if (lineEnd - contentStart > 0) {
						c.length =  lineEnd - c.offset;
						buf.append(d.get(contentStart, lineEnd - contentStart).toCharArray());
					}
				}

				buf.append(TextUtilities.getDefaultLineDelimiter(d));
				StringBuilder reference = null;
				int nonWS = findEndOfWhiteSpace(d, start, lineEnd);
				if (nonWS < c.offset && d.getChar(nonWS) == '{')
					reference = new StringBuilder(d.get(start, nonWS - start));
				else
					reference = indenter.getReferenceIndentation(c.offset);
				if (reference != null)
					buf.append(reference);
				buf.append('}');
				int bound= c.offset > 200 ? c.offset - 200 : CHeuristicScanner.UNBOUND;
				int bracePos = scanner.findOpeningPeer(c.offset - 1, bound, '{', '}');
				if (bracePos != CHeuristicScanner.NOT_FOUND) {
					if (scanner.looksLikeCompositeTypeDefinitionBackward(bracePos, bound) ||
							scanner.previousToken(bracePos - 1, bound) == Symbols.TokenEQUAL) {
						buf.append(';');
					}
				}
			}
			// insert extra line upon new line between two braces
			else if (c.offset > start && contentStart < lineEnd && d.getChar(contentStart) == '}') {
				int firstCharPos = scanner.findNonWhitespaceBackward(c.offset - 1, start);
				if (firstCharPos != CHeuristicScanner.NOT_FOUND && d.getChar(firstCharPos) == '{') {
					c.caretOffset = c.offset + buf.length();
					c.shiftsCaret = false;

					StringBuilder reference = null;
					int nonWS = findEndOfWhiteSpace(d, start, lineEnd);
					if (nonWS < c.offset && d.getChar(nonWS) == '{')
						reference = new StringBuilder(d.get(start, nonWS - start));
					else
						reference = indenter.getReferenceIndentation(c.offset);

					buf.append(TextUtilities.getDefaultLineDelimiter(d));

					if (reference != null)
						buf.append(reference);
				}
			}
			c.text = buf.toString();

		} catch (BadLocationException e) {
			CUIPlugin.log(e);
		}
	}
	
	/**
	 * Computes an insert position for an opening brace if <code>offset</code> maps to a position in
	 * <code>document</code> with a expression in parenthesis that will take a block after the closing parenthesis.
	 *
	 * @param document the document being modified
	 * @param offset the offset of the caret position, relative to the line start.
	 * @param partitioning the document partitioning
	 * @param max the max position
	 * @return an insert position relative to the line start if <code>line</code> contains a parenthesized expression that can be followed by a block, -1 otherwise
	 */
	private static int computeAnonymousPosition(IDocument document, int offset, String partitioning,  int max) {
		// find the opening parenthesis for every closing parenthesis on the current line after offset
		// return the position behind the closing parenthesis if it looks like a method declaration
		// or an expression for an if, while, for, catch statement

		CHeuristicScanner scanner = new CHeuristicScanner(document);
		int pos = offset;
		int length = max;
		int scanTo = scanner.scanForward(pos, length, '}');
		if (scanTo == -1)
			scanTo = length;

		int closingParen = findClosingParenToLeft(scanner, pos) - 1;

		while (true) {
			int startScan = closingParen + 1;
			closingParen = scanner.scanForward(startScan, scanTo, ')');
			if (closingParen == -1)
				break;

			int openingParen = scanner.findOpeningPeer(closingParen - 1, '(', ')');

			// no way an expression at the beginning of the document can mean anything
			if (openingParen < 1)
				break;

			// only select insert positions for parenthesis currently embracing the caret
			if (openingParen > pos)
				continue;
		}

		return -1;
	}

	/**
	 * Finds a closing parenthesis to the left of <code>position</code> in document, where that parenthesis is only
	 * separated by whitespace from <code>position</code>. If no such parenthesis can be found, <code>position</code> is returned.
	 *
	 * @param scanner the C heuristic scanner set up on the document
	 * @param position the first character position in <code>document</code> to be considered
	 * @return the position of a closing parenthesis left to <code>position</code> separated only by whitespace, or <code>position</code> if no parenthesis can be found
	 */
	private static int findClosingParenToLeft(CHeuristicScanner scanner, int position) {
		if (position < 1)
			return position;

		if (scanner.previousToken(position - 1, CHeuristicScanner.UNBOUND) == Symbols.TokenRPAREN)
			return scanner.getPosition() + 1;
		return position;
	}

	private boolean isClosedBrace(IDocument document, int offset, int length) {
		return getBlockBalance(document, offset, fPartitioning) <= 0;
		//TODO: Use smarter algorithm based on
//		CompilationUnitInfo info = getCompilationUnitForMethod(document, offset, fPartitioning);
//		if (info == null)
//			return false;
//
//		CodeReader reader = new CodeReader(info.buffer);
//		ICodeReaderFactory fileCreator = CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE);
//
//		IScanner domScanner = new DOMScanner(reader, new ScannerInfo(), ParserMode.COMPLETE_PARSE,
//				ParserLanguage.C, ParserFactory.createDefaultLogService(),
//				C_GNU_SCANNER_EXTENSION, fileCreator);
//
//		ISourceCodeParser parser = new GNUCPPSourceParser(
//				domScanner,
//				ParserMode.COMPLETE_PARSE,
//				ParserUtil.getParserLogService(),
//				new GPPParserExtensionConfiguration());
//
//		IASTTranslationUnit translationUnit = parser.parse();
//		final int relativeOffset = offset - info.delta;
//	    IASTNode node = translationUnit.selectNodeForLocation(reader.getPath(), relativeOffset, length);
//
//		if (node == null)
//			return false;
//
//		if (node instanceof IASTCompoundStatement) {
//			return getBlockBalance(document, offset, fPartitioning) <= 0;
//		} else if (node instanceof IASTIfStatement) {
//			IASTIfStatement ifStatement = (IASTIfStatement) node;
//			IASTExpression expression = ifStatement.getConditionExpression();
//			IRegion expressionRegion = createRegion(expression, info.delta);
//			IASTStatement thenStatement = ifStatement.getThenClause();
//			IRegion thenRegion = createRegion(thenStatement, info.delta);
//
//			// Between expression and then statement
//			if (expressionRegion.getOffset() + expressionRegion.getLength() <= offset && offset + length <= thenRegion.getOffset())
//				return thenStatement != null;
//
//			IASTStatement elseStatement = ifStatement.getElseClause();
//			IRegion elseRegion = createRegion(elseStatement, info.delta);
//
//			if (elseStatement != null) {
//				int sourceOffset = thenRegion.getOffset() + thenRegion.getLength();
//				int sourceLength = elseRegion.getOffset() - sourceOffset;
//				CHeuristicScanner scanner = new CHeuristicScanner(new SimpleDocument(info.buffer));
//				int pos = sourceOffset;
//				int id;
//				while ((id = scanner.nextToken(pos, sourceOffset + sourceLength - pos)) != CHeuristicScanner.TokenEOF) {
//					if (id == CHeuristicScanner.TokenELSE) {
//						pos = scanner.getPosition();
//						// Between 'else' token and else statement.
//						return pos <= offset && offset + length < elseRegion.getOffset();
//					}
//				}
//
//				return true;
//			}
//		} else if (node instanceof IASTForStatement) {
//			IASTExpression expression = ((IASTForStatement) node).getConditionExpression();
//			IRegion expressionRegion = createRegion(expression, info.delta);
//			IASTStatement body = ((IASTForStatement) node).getBody();
//			IRegion bodyRegion = createRegion(body, info.delta);
//
//			// Between expression and body statement
//			if (expressionRegion.getOffset() + expressionRegion.getLength() <= offset && offset + length <= bodyRegion.getOffset()) {
//				return body != null;
//			}
//		} else if (node instanceof IASTWhileStatement) {
//			IASTExpression expression = ((IASTWhileStatement) node).getCondition();
//			IRegion expressionRegion = createRegion(expression, info.delta);
//			IASTStatement body = ((IASTWhileStatement) node).getBody();
//			IRegion bodyRegion = createRegion(body, info.delta);
//
//			// Between expression and body statement
//			if (expressionRegion.getOffset() + expressionRegion.getLength() <= offset && offset + length <= bodyRegion.getOffset()) {
//				return body != null;
//			}
//		} else if (node instanceof IASTDoStatement) {
//			IASTDoStatement doStatement = (IASTDoStatement) node;
//			IRegion doRegion = createRegion(doStatement, info.delta);
//			IASTStatement body = doStatement.getBody();
//			IRegion bodyRegion = createRegion(body, info.delta);
//
//			// Between 'do' and body statement.
//			if (doRegion.getOffset() + doRegion.getLength() <= offset && offset + length <= bodyRegion.getOffset()) {
//				return body != null;
//			}
//		}
//
//		return true;
	}

    private boolean isLineDelimiter(IDocument document, String text) {
		String[] delimiters = document.getLegalLineDelimiters();
		if (delimiters != null)
			return TextUtilities.equals(delimiters, text) > -1;
		return false;
	}

	/**
	 * Installs a C partitioner with <code>document</code>.
	 *
	 * @param document the document
	 */
	private static void installCPartitioner(Document document) {
		String[] types= new String[] {
		    ICPartitions.C_MULTI_LINE_COMMENT,
			ICPartitions.C_SINGLE_LINE_COMMENT,
			ICPartitions.C_STRING,
			ICPartitions.C_CHARACTER,
			ICPartitions.C_PREPROCESSOR,
			IDocument.DEFAULT_CONTENT_TYPE
		};
		FastPartitioner partitioner= new FastPartitioner(new FastCPartitionScanner(), types);
		partitioner.connect(document);
		document.setDocumentPartitioner(ICPartitions.C_PARTITIONING, partitioner);
	}

	/**
	 * Installs a C partitioner with <code>document</code>.
	 *
	 * @param document the document
	 */
	private static void removeCPartitioner(Document document) {
		document.setDocumentPartitioner(ICPartitions.C_PARTITIONING, null);
	}
	
	private void smartPaste(IDocument document, DocumentCommand command) {
		int newOffset= command.offset;
		int newLength= command.length;
		String newText= command.text;

		try {
			CHeuristicScanner scanner= new CHeuristicScanner(document);
			CIndenter indenter= new CIndenter(document, scanner, fProject);
			int offset= newOffset;

			// reference position to get the indent from
			int refOffset= indenter.findReferencePosition(offset);
			if (refOffset == CHeuristicScanner.NOT_FOUND)
				return;
			int peerOffset= getPeerPosition(document, command);
			peerOffset= indenter.findReferencePosition(peerOffset);
			if (peerOffset == CHeuristicScanner.NOT_FOUND)
				return;
			refOffset= Math.min(refOffset, peerOffset);

			// eat any WS before the insertion to the beginning of the line
			int firstLine= 1; // don't format the first line per default, as it has other content before it
			IRegion line= document.getLineInformationOfOffset(offset);
			String notSelected= document.get(line.getOffset(), offset - line.getOffset());
			if (notSelected.trim().length() == 0) {
				newLength += notSelected.length();
				newOffset= line.getOffset();
				firstLine= 0;
			}

			// 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
			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
			// 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.
			boolean isIndentDetected= false;
			StringBuilder addition= new StringBuilder();
			int insertLength= 0;
			int first= document.computeNumberOfLines(prefix) + firstLine; // don't format first line
			int lines= temp.getNumberOfLines();
			boolean changed= false;
			boolean indentInsideLineComments= IndentUtil.indentInsideLineComments(fProject);

			for (int l= first; l < lines; l++) { // we don't change the number of lines while adding indents
				IRegion r= temp.getLineInformation(l);
				int lineOffset= r.getOffset();
				int lineLength= r.getLength();

				if (lineLength == 0) // don't modify empty lines
					continue;

				if (!isIndentDetected) {
					// indent the first pasted line
					String current= IndentUtil.getCurrentIndent(temp, l, indentInsideLineComments);
					StringBuilder correct= new StringBuilder(IndentUtil.computeIndent(temp, l, indenter, scanner));

					insertLength= subtractIndent(correct, current, addition);
					// workaround for bug 181139
					if (/*l != first && */temp.get(lineOffset, lineLength).trim().length() != 0) {
						isIndentDetected= true;
						if (insertLength == 0) {
							 // no adjustment needed, bail out
							if (firstLine == 0) {
								// but we still need to adjust the first line
								command.offset= newOffset;
								command.length= newLength;
								if (changed)
									break; // still need to get the leading indent of the first line
							}
							return;
						}
						removeCPartitioner(temp);
					} else {
						changed= insertLength != 0;
					}
				}

				// relatively indent all pasted lines
				if (insertLength > 0)
					addIndent(temp, l, addition, indentInsideLineComments);
				else if (insertLength < 0)
					cutIndent(temp, l, -insertLength, indentInsideLineComments);
			}

			temp.stopRewriteSession(session);
			newText= temp.get(prefix.length(), temp.getLength() - prefix.length());

			command.offset= newOffset;
			command.length= newLength;
			command.text= newText;
		} catch (BadLocationException e) {
			CUIPlugin.log(e);
		}
	}

	/**
	 * Computes the difference of two indentations and returns the difference in
	 * length of current and correct. If the return value is positive, <code>addition</code>
	 * is initialized with a substring of that length of <code>correct</code>.
	 *
	 * @param correct the correct indentation
	 * @param current the current indentation (migth contain non-whitespace)
	 * @param difference a string buffer - if the return value is positive, it will be cleared and set to the substring of <code>current</code> of that length
	 * @return the difference in lenght of <code>correct</code> and <code>current</code>
	 */
	private int subtractIndent(CharSequence correct, CharSequence current, StringBuilder difference) {
		int c1= computeVisualLength(correct);
		int c2= computeVisualLength(current);
		int diff= c1 - c2;
		if (diff <= 0)
			return diff;

		difference.setLength(0);
		int len= 0, i= 0;
		while (len < diff) {
			char c= correct.charAt(i++);
			difference.append(c);
			len += computeVisualLength(c);
		}

		return diff;
	}

	/**
	 * Indents line <code>line</code> in <code>document</code> with <code>indent</code>.
	 * Leaves leading comment signs alone.
	 *
	 * @param document the document
	 * @param line the line
	 * @param indent the indentation to insert
	 * @param indentInsideLineComments option whether to indent inside line comments starting at column 0
	 * @throws BadLocationException on concurrent document modification
	 */
	private static void addIndent(Document document, int line, CharSequence indent, boolean indentInsideLineComments) throws BadLocationException {
		IRegion region= document.getLineInformation(line);
		int insert= region.getOffset();
		int endOffset= region.getOffset() + region.getLength();

		if (indentInsideLineComments) {
			// go behind line comments
			while (insert < endOffset - 2 && document.get(insert, 2).equals(LINE_COMMENT))
				insert += 2;
		}

		// insert indent
		document.replace(insert, 0, indent.toString());
	}

	/**
	 * Cuts the visual equivalent of <code>toDelete</code> characters out of the
	 * indentation of line <code>line</code> in <code>document</code>. Leaves
	 * leading comment signs alone.
	 *
	 * @param document the document
	 * @param line the line
	 * @param toDelete the number of space equivalents to delete.
	 * @param indentInsideLineComments option whether to indent inside line comments starting at column 0
	 * @throws BadLocationException on concurrent document modification
	 */
	private void cutIndent(Document document, int line, int toDelete, boolean indentInsideLineComments) throws BadLocationException {
		IRegion region= document.getLineInformation(line);
		int from= region.getOffset();
		int endOffset= region.getOffset() + region.getLength();

		if (indentInsideLineComments) {
			// go behind line comments
			while (from < endOffset - 2 && document.get(from, 2).equals(LINE_COMMENT))
				from += 2;
		}

		int to= from;
		while (toDelete > 0 && to < endOffset) {
			char ch= document.getChar(to);
			if (!Character.isWhitespace(ch))
				break;
			toDelete -= computeVisualLength(ch);
			if (toDelete >= 0)
				to++;
			else
				break;
		}

		document.replace(from, to - from, null);
	}

	/**
	 * Returns the visual length of a given <code>CharSequence</code> taking into
	 * account the visual tabulator length.
	 *
	 * @param seq the string to measure
	 * @return the visual length of <code>seq</code>
	 */
	private int computeVisualLength(CharSequence seq) {
		int size= 0;
		int tablen= getVisualTabLengthPreference();

		for (int i= 0; i < seq.length(); i++) {
			char ch= seq.charAt(i);
			if (ch == '\t') {
				if (tablen != 0)
					size += tablen - size % tablen;
				// else: size stays the same
			} else {
				size++;
			}
		}
		return size;
	}

	/**
	 * Returns the visual length of a given character taking into
	 * account the visual tabulator length.
	 *
	 * @param ch the character to measure
	 * @return the visual length of <code>ch</code>
	 */
	private int computeVisualLength(char ch) {
		if (ch == '\t')
			return getVisualTabLengthPreference();
		return 1;
	}

	/**
	 * The preference setting for the visual tabulator display.
	 *
	 * @return the number of spaces displayed for a tabulator in the editor
	 */
	private int getVisualTabLengthPreference() {
		return CodeFormatterUtil.getTabWidth(fProject);
	}

	private int getPeerPosition(IDocument document, DocumentCommand command) {
		if (document.getLength() == 0)
			return 0;
    	/*
    	 * Search for scope closers in the pasted text and find their opening peers
    	 * in the document.
    	 */
    	Document pasted= new Document(command.text);
    	installCPartitioner(pasted);
    	int firstPeer= command.offset;

    	CHeuristicScanner pScanner= new CHeuristicScanner(pasted);
    	CHeuristicScanner dScanner= new CHeuristicScanner(document);

    	// add scope relevant after context to peer search
    	int afterToken= dScanner.nextToken(command.offset + command.length, CHeuristicScanner.UNBOUND);
    	try {
			switch (afterToken) {
			case Symbols.TokenRBRACE:
				pasted.replace(pasted.getLength(), 0, "}"); //$NON-NLS-1$
				break;
			case Symbols.TokenRPAREN:
				pasted.replace(pasted.getLength(), 0, ")"); //$NON-NLS-1$
				break;
			case Symbols.TokenRBRACKET:
				pasted.replace(pasted.getLength(), 0, "]"); //$NON-NLS-1$
				break;
			}
		} catch (BadLocationException e) {
			// cannot happen
			Assert.isTrue(false);
		}

    	int pPos= 0; // paste text position (increasing from 0)
    	int dPos= Math.max(0, command.offset - 1); // document position (decreasing from paste offset)
    	while (true) {
    		int token= pScanner.nextToken(pPos, CHeuristicScanner.UNBOUND);
   			pPos= pScanner.getPosition();
    		switch (token) {
    			case Symbols.TokenLBRACE:
    			case Symbols.TokenLBRACKET:
    			case Symbols.TokenLPAREN:
    				pPos= skipScope(pScanner, pPos, token);
    				if (pPos == CHeuristicScanner.NOT_FOUND)
    					return firstPeer;
    				break; // closed scope -> keep searching
    			case Symbols.TokenRBRACE:
    				int peer= dScanner.findOpeningPeer(dPos, '{', '}');
    				dPos= peer - 1;
    				if (peer == CHeuristicScanner.NOT_FOUND)
    					return firstPeer;
    				firstPeer= peer;
    				break; // keep searching
    			case Symbols.TokenRBRACKET:
    				peer= dScanner.findOpeningPeer(dPos, '[', ']');
    				dPos= peer - 1;
    				if (peer == CHeuristicScanner.NOT_FOUND)
    					return firstPeer;
    				firstPeer= peer;
    				break; // keep searching
    			case Symbols.TokenRPAREN:
    				peer= dScanner.findOpeningPeer(dPos, '(', ')');
    				dPos= peer - 1;
    				if (peer == CHeuristicScanner.NOT_FOUND)
    					return firstPeer;
    				firstPeer= peer;
    				break; // keep searching
    				
    			case Symbols.TokenCASE:
    			case Symbols.TokenDEFAULT:
    			    {
    					CIndenter indenter= new CIndenter(document, dScanner, fProject);
    					peer= indenter.findReferencePosition(dPos, false, MatchMode.MATCH_CASE);
    					if (peer == CHeuristicScanner.NOT_FOUND)
    						return firstPeer;
    					firstPeer= peer;
    				}
    				break; // keep searching

    			case Symbols.TokenPUBLIC:
    			case Symbols.TokenPROTECTED:
    			case Symbols.TokenPRIVATE:
				    {
						CIndenter indenter= new CIndenter(document, dScanner, fProject);
						peer= indenter.findReferencePosition(dPos, false, MatchMode.MATCH_ACCESS_SPECIFIER);
						if (peer == CHeuristicScanner.NOT_FOUND)
							return firstPeer;
						firstPeer= peer;
					}
    				break; // keep searching
    				
    			case Symbols.TokenEOF:
    				return firstPeer;
    			default:
    				// keep searching
    		}
    	}
    }

    /**
     * Skips the scope opened by <code>token</code> in <code>document</code>,
     * returns either the position of the
     * @param pos
     * @param token
     * @return the position after the scope
     */
    private static int skipScope(CHeuristicScanner scanner, int pos, int token) {
    	int openToken= token;
    	int closeToken;
    	switch (token) {
    		case Symbols.TokenLPAREN:
    			closeToken= Symbols.TokenRPAREN;
    			break;
    		case Symbols.TokenLBRACKET:
    			closeToken= Symbols.TokenRBRACKET;
    			break;
    		case Symbols.TokenLBRACE:
    			closeToken= Symbols.TokenRBRACE;
    			break;
    		default:
    			Assert.isTrue(false);
    			return -1; // dummy
    	}

    	int depth= 1;
    	int p= pos;

    	while (true) {
    		int tok= scanner.nextToken(p, CHeuristicScanner.UNBOUND);
    		p= scanner.getPosition();

    		if (tok == openToken) {
    			depth++;
    		} else if (tok == closeToken) {
    			depth--;
    			if (depth == 0)
    				return p + 1;
    		} else if (tok == Symbols.TokenEOF) {
    			return CHeuristicScanner.NOT_FOUND;
    		}
    	}
    }

	private void smartIndentOnKeypress(IDocument document, DocumentCommand command) {
		switch (command.text.charAt(0)) {
			case '}':
				smartIndentAfterClosingBracket(document, command);
				break;
			case '{':
				smartIndentAfterOpeningBracket(document, command);
				break;
			case 'e':
				smartIndentUponE(document, command);
				break;
			case ':':
				smartIndentAfterColumn(document, command);
				break;
			case '#':
				smartIndentAfterHash(document, command);
				break;
		}
	}

	private void smartIndentUponE(IDocument doc, DocumentCommand c) {
		if (c.offset < 4 || doc.getLength() == 0)
			return;

		try {
			String content = doc.get(c.offset - 3, 3);
			if (content.equals("els")) { //$NON-NLS-1$
				CHeuristicScanner scanner = new CHeuristicScanner(doc);
				int p = c.offset - 3;

				// current line
				int line = doc.getLineOfOffset(p);
				int lineOffset = doc.getLineOffset(line);

				// make sure we don't have any leading comments etc.
				if (doc.get(lineOffset, p - lineOffset).trim().length() != 0)
					return;

				// Line of last C code
				int pos = scanner.findNonWhitespaceBackward(p - 1, CHeuristicScanner.UNBOUND);
				if (pos == -1)
					return;
				int lastLine = doc.getLineOfOffset(pos);

				// Only shift if the last C line is further up and is a braceless block candidate
				if (lastLine < line) {
					CIndenter indenter = new CIndenter(doc, scanner, fProject);
					int ref = indenter.findReferencePosition(p, true, MatchMode.REGULAR);
					if (ref == CHeuristicScanner.NOT_FOUND)
						return;
					int refLine = doc.getLineOfOffset(ref);
					String indent = getIndentOfLine(doc, refLine);

					if (indent != null) {
						c.text = indent.toString() + "else"; //$NON-NLS-1$
						c.length += c.offset - lineOffset;
						c.offset = lineOffset;
					}
				}

				return;
			}

			if (content.equals("cas")) { //$NON-NLS-1$
				CHeuristicScanner scanner = new CHeuristicScanner(doc);
				int p = c.offset - 3;

				// current line
				int line = doc.getLineOfOffset(p);
				int lineOffset = doc.getLineOffset(line);

				// make sure we don't have any leading comments etc.
				if (doc.get(lineOffset, p - lineOffset).trim().length() != 0)
					return;

				// Line of last C code
				int pos = scanner.findNonWhitespaceBackward(p - 1, CHeuristicScanner.UNBOUND);
				if (pos == -1)
					return;
				int lastLine = doc.getLineOfOffset(pos);

				// Only shift if the last C line is further up and is a braceless block candidate
				if (lastLine < line) {
					CIndenter indenter = new CIndenter(doc, scanner, fProject);
					int ref = indenter.findReferencePosition(p, false, MatchMode.MATCH_CASE);
					if (ref == CHeuristicScanner.NOT_FOUND)
						return;
					int refLine = doc.getLineOfOffset(ref);
					int nextToken = scanner.nextToken(ref, CHeuristicScanner.UNBOUND);
					String indent;
					if (nextToken == Symbols.TokenCASE || nextToken == Symbols.TokenDEFAULT)
						indent = getIndentOfLine(doc, refLine);
					else // at the brace of the switch
						indent = indenter.computeIndentation(p).toString();

					if (indent != null) {
						c.text = indent.toString() + "case"; //$NON-NLS-1$
						c.length += c.offset - lineOffset;
						c.offset = lineOffset;
					}
				}

				return;
			}
		} catch (BadLocationException e) {
			CUIPlugin.log(e);
		}
	}

	private void smartIndentAfterColumn(IDocument doc, DocumentCommand c) {
		try {
			int offset = c.offset;
			// Current line
			int line = doc.getLineOfOffset(offset);
			IRegion startLine = doc.getLineInformationOfOffset(offset);
			int lineOffset = startLine.getOffset();

			CHeuristicScanner scanner = new CHeuristicScanner(doc);
			int prevToken = scanner.previousToken(offset - 1, lineOffset);
			switch (prevToken) {
				case Symbols.TokenDEFAULT:
				case Symbols.TokenPUBLIC:
				case Symbols.TokenPROTECTED:
				case Symbols.TokenPRIVATE:
					break;
					
				default:
					return;
			}
			
			int p = scanner.getPosition() + 1;

			// Make sure we don't have any leading comments etc.
			if (doc.get(lineOffset, p - lineOffset).trim().length() != 0)
				return;
			
			// Line of last C code
			int pos = scanner.findNonWhitespaceBackward(p - 1, CHeuristicScanner.UNBOUND);
			if (pos == -1)
				return;
			int lastLine = doc.getLineOfOffset(pos);

			// Only shift if the last C line is further up and is a braceless block candidate
			if (lastLine < line) {
				CIndenter indenter = new CIndenter(doc, scanner, fProject);
				int ref;
				if (prevToken == Symbols.TokenDEFAULT)
					ref = indenter.findReferencePosition(p, false, MatchMode.MATCH_CASE);
				else
					ref = indenter.findReferencePosition(p, false, MatchMode.MATCH_ACCESS_SPECIFIER);
				if (ref == CHeuristicScanner.NOT_FOUND)
					return;
				int refLine = doc.getLineOfOffset(ref);
				int nextToken = scanner.nextToken(ref, CHeuristicScanner.UNBOUND);
				String indent;
				if (nextToken == Symbols.TokenCASE || nextToken == Symbols.TokenDEFAULT ||
						nextToken == Symbols.TokenPUBLIC || nextToken == Symbols.TokenPROTECTED ||
						nextToken == Symbols.TokenPRIVATE) {
					indent = getIndentOfLine(doc, refLine);
				} else { // at the brace of the switch or the class
					indent = indenter.computeIndentation(p).toString();
				}

				if (indent != null) {
					c.text = indent.toString() + doc.get(p, offset - p) + c.text;
					c.length += c.offset - lineOffset;
					c.offset = lineOffset;
				}
			}

			return;
		} catch (BadLocationException e) {
			CUIPlugin.log(e);
		}
	}

	private void smartIndentAfterHash(IDocument doc, DocumentCommand c) {
		try {
			ITypedRegion partition= TextUtilities.getPartition(doc, fPartitioning, c.offset, false);
			if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) {
				IRegion startLine= doc.getLineInformationOfOffset(c.offset);
				String indent= doc.get(startLine.getOffset(), c.offset - startLine.getOffset());
				if (indent.trim().length() == 0) {
					c.offset -= indent.length();
					c.length += indent.length();
				}
			}
		} catch (BadLocationException e) {
			CUIPlugin.log(e);
		}
	}

	@Override
	public void customizeDocumentCommand(IDocument d, DocumentCommand c) {
		if (!c.doit)
			return;

		clearCachedValues();
		if (!fIsSmartMode) {
			super.customizeDocumentCommand(d, c);
			return;
		}
		
		boolean isNewLine= c.length == 0 && c.text != null && isLineDelimiter(d, c.text);
		if (isNewLine) {
			smartIndentAfterNewLine(d, c);
		} else if (c.text.length() == 1 && getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_AUTO_INDENT)) {
			smartIndentOnKeypress(d, c);
		} else if (c.text.length() > 1
				&& getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_PASTE)
				&& c.text.trim().length() != 0) {
			smartPaste(d, c); // no smart backspace for paste
		}
	}

	private static IPreferenceStore getPreferenceStore() {
		return CUIPlugin.getDefault().getCombinedPreferenceStore();
	}

	private void clearCachedValues() {
        IPreferenceStore preferenceStore = getPreferenceStore();
		fCloseBrace = preferenceStore.getBoolean(PreferenceConstants.EDITOR_CLOSE_BRACES);
		fIsSmartMode = computeSmartMode();
	}

	private boolean computeSmartMode() {
		IWorkbenchPage page = CUIPlugin.getActivePage();
		if (page != null)  {
			IEditorPart part = page.getActiveEditor();
			if (part instanceof MultiPageEditorPart) {
				part= (IEditorPart)part.getAdapter(ITextEditorExtension3.class);
			}
			if (part instanceof ITextEditorExtension3) {
				ITextEditorExtension3 extension = (ITextEditorExtension3) part;
				return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT;
			}
			if (part == null) {
				// TODO: Remove this if statement once CAutoIndentTest is fixed so that getActiveEditor does not return null.
				return true;
			}
		}
		return false;
	}

//	private static CompilationUnitInfo getCompilationUnitForMethod(IDocument document, int offset, String partitioning) {
//		try {
//			CHeuristicScanner scanner = new CHeuristicScanner(document);
//
//			IRegion sourceRange = scanner.findSurroundingBlock(offset);
//			if (sourceRange == null)
//				return null;
//			String source = document.get(sourceRange.getOffset(), sourceRange.getLength());
//
//			StringBuilder contents = new StringBuilder();
//			contents.append("class ____C{void ____m()"); //$NON-NLS-1$
//			final int methodOffset = contents.length();
//			contents.append(source);
//			contents.append("};"); //$NON-NLS-1$
//
//			char[] buffer = contents.toString().toCharArray();
//			return new CompilationUnitInfo(buffer, sourceRange.getOffset() - methodOffset);
//		} catch (BadLocationException e) {
//			CUIPlugin.log(e);
//		}
//
//		return null;
//	}

	/**
	 * Returns the block balance, i.e. zero if the blocks are balanced at
	 * <code>offset</code>, a negative number if there are more closing than opening
	 * braces, and a positive number if there are more opening than closing braces.
	 *
	 * @param document
	 * @param offset
	 * @param partitioning
	 * @return the block balance
	 */
	private static int getBlockBalance(IDocument document, int offset, String partitioning) {
		if (offset < 1)
			return -1;
		if (offset >= document.getLength())
			return 1;

		int begin = offset;
		int end = offset - 1;

		CHeuristicScanner scanner = new CHeuristicScanner(document);

		while (true) {
			begin = scanner.findOpeningPeer(begin - 1, '{', '}');
			end = scanner.findClosingPeer(end + 1, '{', '}');
			if (begin == -1 && end == -1)
				return 0;
			if (begin == -1)
				return -1;
			if (end == -1)
				return 1;
		}
	}

//	private static IRegion createRegion(IASTNode node, int delta) {
//		IASTNodeLocation nodeLocation = node.getNodeLocations()[0];
//		return node == null ? null : new Region(nodeLocation.getNodeOffset() + delta, nodeLocation.getNodeLength());
//	}
}

Back to the top