The R Project SVN R-packages

Rev

Rev 6242 | Rev 6246 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6242 Rev 6245
Line 194... Line 194...
194
		[self updateLineWrappingMode];
194
		[self updateLineWrappingMode];
195
 
195
 
196
	// Re-define tab stops for a better editing
196
	// Re-define tab stops for a better editing
197
	[self setTabStops];
197
	[self setTabStops];
198
 
198
 
199
	// add NSViewBoundsDidChangeNotification to scrollView
-
 
200
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsDidChangeNotification:) name:NSViewBoundsDidChangeNotification object:[scrollView contentView]];
-
 
201
 
-
 
202
	NSColor *c = [Preferences unarchivedObjectForKey:normalSyntaxColorKey withDefault:nil];
199
	NSColor *c = [Preferences unarchivedObjectForKey:normalSyntaxColorKey withDefault:nil];
203
	if (c) shColorNormal = c;
200
	if (c) shColorNormal = c;
204
	else shColorNormal=[NSColor colorWithDeviceRed:0.025 green:0.085 blue:0.600 alpha:1.0];
201
	else shColorNormal=[NSColor colorWithDeviceRed:0.025 green:0.085 blue:0.600 alpha:1.0];
205
	[shColorNormal retain];
202
	[shColorNormal retain];
206
 
203
 
Line 318... Line 315...
318
	}
315
	}
319
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
316
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
320
	[[self layoutManager] setAllowsNonContiguousLayout:YES];
317
	[[self layoutManager] setAllowsNonContiguousLayout:YES];
321
#endif
318
#endif
322
 
319
 
-
 
320
	// add NSViewBoundsDidChangeNotification to scrollView
-
 
321
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsDidChangeNotification:) name:NSViewBoundsDidChangeNotification object:[scrollView contentView]];
-
 
322
 
323
}
323
}
324
 
324
 
325
- (void)dealloc {
325
- (void)dealloc {
326
	SLog(@"RScriptEditorTextView: dealloc <%@>", self);
326
	SLog(@"RScriptEditorTextView: dealloc <%@>", self);
327
 
327
 
Line 600... Line 600...
600
	// if the user really changed the text
600
	// if the user really changed the text
601
	if(editedMask != 1) {
601
	if(editedMask != 1) {
602
 
602
 
603
		// For larger text break a running syntax highlighting for user interaction
603
		// For larger text break a running syntax highlighting for user interaction
604
		// to make them more responsive (typing and scrolling)
604
		// to make them more responsive (typing and scrolling)
605
		if([[theTextStorage string] length] > 120000) {
605
		// if([[theTextStorage string] length] > 120000) {
606
			breakSyntaxHighlighting = 1;
606
		// 	breakSyntaxHighlighting = 1;
607
		}
607
		// }
608
 
608
 
609
		[self checkSnippets];
609
		[self checkSnippets];
610
 
610
 
-
 
611
		breakSyntaxHighlighting = 1;
-
 
612
 
611
		// Cancel calling doSyntaxHighlighting
613
		// Cancel calling doSyntaxHighlighting
612
		[NSObject cancelPreviousPerformRequestsWithTarget:self 
614
		[NSObject cancelPreviousPerformRequestsWithTarget:self 
613
								selector:@selector(doSyntaxHighlighting) 
615
								selector:@selector(doSyntaxHighlighting) 
614
								object:nil];
616
								object:nil];
615
 
617
 
Line 696... Line 698...
696
	[self setEditable:oldEditableStatus];
698
	[self setEditable:oldEditableStatus];
697
 
699
 
698
	[paragraphStyle release];
700
	[paragraphStyle release];
699
}
701
}
700
 
702
 
-
 
703
- (BOOL)isSyntaxHighlighting
-
 
704
{
-
 
705
	return isSyntaxHighlighting;
-
 
706
}
-
 
707
 
-
 
708
- (BOOL)breakSyntaxHighlighting
-
 
709
{
-
 
710
	return breakSyntaxHighlighting;
-
 
711
}
701
/**
712
/**
702
 * Syntax Highlighting.
713
 * Syntax Highlighting.
703
 *  
714
 *  
704
 * (The main bottleneck is the [NSTextStorage addAttribute:value:range:] method - the parsing itself is really fast!)
715
 * (The main bottleneck is the [NSTextStorage addAttribute:value:range:] method - the parsing itself is really fast!)
705
 * Some sample code from Andrew Choi ( http://members.shaw.ca/akochoi-old/blog/2003/11-09/index.html#3 ) has been reused.
716
 * Some sample code from Andrew Choi ( http://members.shaw.ca/akochoi-old/blog/2003/11-09/index.html#3 ) has been reused.
706
 */
717
 */
707
- (void)doSyntaxHighlighting
718
- (void)doSyntaxHighlighting
708
{
719
{
709
 
720
 
-
 
721
	if(!syntaxHighlightingEnabled)
-
 
722
		breakSyntaxHighlighting = 0;
-
 
723
 
710
	if (!syntaxHighlightingEnabled || [[self delegate] plain]) return;
724
	if (!syntaxHighlightingEnabled || [[self delegate] plain]) return;
711
 
725
 
712
	isSyntaxHighlighting = YES;
726
	isSyntaxHighlighting = YES;
713
 
727
 
714
	NSString *selfstr    = [theTextStorage string];
728
	NSString *selfstr    = [theTextStorage string];
715
	NSInteger strlength  = (NSInteger)[selfstr length];
729
	NSInteger strlength  = (NSInteger)[selfstr length];
716
 
730
 
717
	// do not highlight if text larger than 10MB
731
	// do not highlight if text larger than 10MB
718
	if(strlength > 10000000 || !strlength) return;
732
	if(strlength > 10000000 || !strlength) {
-
 
733
		isSyntaxHighlighting = NO;
-
 
734
		breakSyntaxHighlighting = 0;
-
 
735
		return;
-
 
736
	}
719
 
737
 
720
	// == Do highlighting partly (max R_SYNTAX_HILITE_BIAS*2 around visibleRange
738
	// == Do highlighting partly (max R_SYNTAX_HILITE_BIAS*2 around visibleRange
721
	// by considering entire lines).
739
	// by considering entire lines).
722
 
740
 
723
	// Get the text range currently displayed in the view port
741
	// Get the text range currently displayed in the view port
724
	NSRect visibleRect = [self visibleRect];
742
	NSRect visibleRect = [self visibleRect];
725
	NSRange visibleRange = [[self layoutManager] glyphRangeForBoundingRectWithoutAdditionalLayout:visibleRect inTextContainer:[self textContainer]];
743
	NSRange visibleRange = [[self layoutManager] glyphRangeForBoundingRectWithoutAdditionalLayout:visibleRect inTextContainer:[self textContainer]];
726
 
744
 
727
	if(!visibleRange.length) {
745
	if(!visibleRange.length) {
728
		isSyntaxHighlighting = NO;
746
		isSyntaxHighlighting = NO;
-
 
747
		breakSyntaxHighlighting = 0;
729
		return;
748
		return;
730
	}
749
	}
731
 
750
 
-
 
751
	NSInteger breakCounter = 1000;
732
	NSInteger start = visibleRange.location - R_SYNTAX_HILITE_BIAS;
752
	NSInteger start = visibleRange.location - R_SYNTAX_HILITE_BIAS;
733
	if (start > 0)
753
	if (start > 0)
734
		while(start > 0) {
754
		while(start > 0) {
-
 
755
			if(!breakCounter--) break;
735
			if(CFStringGetCharacterAtIndex((CFStringRef)selfstr, start)=='\n')
756
			if(CFStringGetCharacterAtIndex((CFStringRef)selfstr, start)=='\n')
736
				break;
757
				break;
737
			start--;
758
			start--;
738
		}
759
		}
739
	if(start < 0) start = 0;
760
	if(start < 0) start = 0;
-
 
761
	breakCounter = 1000;
740
	NSInteger end = NSMaxRange(visibleRange) + R_SYNTAX_HILITE_BIAS;
762
	NSInteger end = NSMaxRange(visibleRange) + R_SYNTAX_HILITE_BIAS;
741
	if (end > strlength) {
763
	if (end > strlength) {
742
		end = strlength;
764
		end = strlength;
743
	} else {
765
	} else {
744
		while(end < strlength) {
766
		while(end < strlength) {
-
 
767
			if(!breakCounter--) break;
745
			if(CFStringGetCharacterAtIndex((CFStringRef)selfstr, end)=='\n')
768
			if(CFStringGetCharacterAtIndex((CFStringRef)selfstr, end)=='\n')
746
				break;
769
				break;
747
			end++;
770
			end++;
748
		}
771
		}
749
	}
772
	}
Line 751... Line 774...
751
	NSRange textRange = NSMakeRange(start, end-start);
774
	NSRange textRange = NSMakeRange(start, end-start);
752
 
775
 
753
	// only to be sure that nothing went wrongly
776
	// only to be sure that nothing went wrongly
754
	textRange = NSIntersectionRange(textRange, NSMakeRange(0, [theTextStorage length])); 
777
	textRange = NSIntersectionRange(textRange, NSMakeRange(0, [theTextStorage length])); 
755
 
778
 
756
	if (!textRange.length) {
779
	if (!textRange.length || textRange.length > 30000) {
757
		isSyntaxHighlighting = NO;
780
		isSyntaxHighlighting = NO;
-
 
781
		breakSyntaxHighlighting = 0;
758
		return;
782
		return;
759
	}
783
	}
760
 
784
 
761
	[theTextStorage beginEditing];
785
	[theTextStorage beginEditing];
762
 
786
 
Line 815... Line 839...
815
					// Cancel calling doSyntaxHighlighting
839
					// Cancel calling doSyntaxHighlighting
816
					[NSObject cancelPreviousPerformRequestsWithTarget:self 
840
					[NSObject cancelPreviousPerformRequestsWithTarget:self 
817
											selector:@selector(doSyntaxHighlighting) 
841
											selector:@selector(doSyntaxHighlighting) 
818
											object:nil];
842
											object:nil];
819
 
843
 
820
					[self performSelector:@selector(doSyntaxHighlighting) withObject:nil afterDelay:0.15f];
844
					[self performSelector:@selector(doSyntaxHighlighting) withObject:nil afterDelay:0.08f];
821
 
845
 
822
					breakSyntaxHighlighting = 0;
846
					breakSyntaxHighlighting = 0;
823
					break;
847
					break;
824
 
848
 
825
				}
849
				}
Line 871... Line 895...
871
				// Cancel calling doSyntaxHighlighting
895
				// Cancel calling doSyntaxHighlighting
872
				[NSObject cancelPreviousPerformRequestsWithTarget:self 
896
				[NSObject cancelPreviousPerformRequestsWithTarget:self 
873
										selector:@selector(doSyntaxHighlighting) 
897
										selector:@selector(doSyntaxHighlighting) 
874
										object:nil];
898
										object:nil];
875
				
899
				
876
				[self performSelector:@selector(doSyntaxHighlighting) withObject:nil afterDelay:0.2f];
900
				[self performSelector:@selector(doSyntaxHighlighting) withObject:nil afterDelay:0.08f];
877
 
901
 
878
				breakSyntaxHighlighting = 0;
902
				breakSyntaxHighlighting = 0;
879
				break;
903
				break;
880
 
904
 
881
			}
905
			}
Line 894... Line 918...
894
		if(c) [typeAttr setObject:c forKey:NSForegroundColorAttributeName];
918
		if(c) [typeAttr setObject:c forKey:NSForegroundColorAttributeName];
895
		[self setTypingAttributes:typeAttr];
919
		[self setTypingAttributes:typeAttr];
896
	}
920
	}
897
 
921
 
898
	[theTextStorage endEditing];
922
	[theTextStorage endEditing];
899
	isSyntaxHighlighting = NO;
-
 
-
 
923
 
900
	[self setNeedsDisplayInRect:visibleRect];
924
	[self setNeedsDisplayInRect:visibleRect];
901
 
925
 
-
 
926
	breakSyntaxHighlighting = 0;
-
 
927
 
-
 
928
	isSyntaxHighlighting = NO;
-
 
929
 
902
}
930
}
903
 
931
 
904
-(void)resetHighlights
932
-(void)resetHighlights
905
{
933
{
906
 
934
 
Line 960... Line 988...
960
- (void)boundsDidChangeNotification:(NSNotification *)notification
988
- (void)boundsDidChangeNotification:(NSNotification *)notification
961
{
989
{
962
 
990
 
963
	if(startListeningToBoundChanges) {
991
	if(startListeningToBoundChanges) {
964
 
992
 
-
 
993
		breakSyntaxHighlighting = 1;
-
 
994
 
965
		[NSObject cancelPreviousPerformRequestsWithTarget:self 
995
		[NSObject cancelPreviousPerformRequestsWithTarget:self 
966
									selector:@selector(doSyntaxHighlighting) 
996
									selector:@selector(doSyntaxHighlighting) 
967
									object:nil];
997
									object:nil];
968
 
998
 
969
		if(![theTextStorage changeInLength]) {
999
		if(![theTextStorage changeInLength]) {
970
			if([[theTextStorage string] length] > 120000)
1000
			if([[theTextStorage string] length] > 120000)
971
				breakSyntaxHighlighting = 2;
1001
				breakSyntaxHighlighting = 2;
972
			[self performSelector:@selector(doSyntaxHighlighting) withObject:nil afterDelay:0.05];
1002
			[self performSelector:@selector(doSyntaxHighlighting) withObject:nil afterDelay:0.08];
973
		}
1003
		}
974
		if(lineNumberingEnabled) {
1004
		if(lineNumberingEnabled) {
975
			[NSObject cancelPreviousPerformRequestsWithTarget:[[self enclosingScrollView] verticalRulerView] 
1005
			[NSObject cancelPreviousPerformRequestsWithTarget:[[self enclosingScrollView] verticalRulerView] 
976
										selector:@selector(refresh) 
1006
										selector:@selector(refresh) 
977
										object:nil];
1007
										object:nil];
Line 1239... Line 1269...
1239
	[theTextStorage ensureAttributesAreFixedInRange:r];
1269
	[theTextStorage ensureAttributesAreFixedInRange:r];
1240
 
1270
 
1241
 
1271
 
1242
	if(lineNumberingEnabled)
1272
	if(lineNumberingEnabled)
1243
		[[[self enclosingScrollView] verticalRulerView] performSelector:@selector(refresh) withObject:nil afterDelay:0.0f];
1273
		[[[self enclosingScrollView] verticalRulerView] performSelector:@selector(refresh) withObject:nil afterDelay:0.0f];
-
 
1274
 
-
 
1275
	breakSyntaxHighlighting = 1;
1244
	
1276
	
1245
	[NSObject cancelPreviousPerformRequestsWithTarget:self 
1277
	[NSObject cancelPreviousPerformRequestsWithTarget:self 
1246
							selector:@selector(doSyntaxHighlighting) 
1278
							selector:@selector(doSyntaxHighlighting) 
1247
							object:nil];
1279
							object:nil];
1248
 
1280
 
1249
	[self performSelector:@selector(doSyntaxHighlighting) withObject:nil afterDelay:0.0f];
1281
	[self performSelector:@selector(doSyntaxHighlighting) withObject:nil afterDelay:0.01f];
1250
}
1282
}
1251
 
1283
 
1252
- (void)refoldLinesInRange:(NSRange)range
1284
- (void)refoldLinesInRange:(NSRange)range
1253
{
1285
{
1254
	NSInteger foldId = [theTextStorage registerFoldedRange:range];
1286
	NSInteger foldId = [theTextStorage registerFoldedRange:range];