The R Project SVN R-packages

Rev

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

Rev 6230 Rev 6231
Line 566... Line 566...
566
		// Cancel calling doSyntaxHighlighting
566
		// Cancel calling doSyntaxHighlighting
567
		[NSObject cancelPreviousPerformRequestsWithTarget:self 
567
		[NSObject cancelPreviousPerformRequestsWithTarget:self 
568
								selector:@selector(doSyntaxHighlighting) 
568
								selector:@selector(doSyntaxHighlighting) 
569
								object:nil];
569
								object:nil];
570
 
570
 
571
		[self performSelector:@selector(doSyntaxHighlighting) withObject:nil afterDelay:0.01f];
571
		[self performSelector:@selector(doSyntaxHighlighting) withObject:nil afterDelay:0.05f];
572
 
572
 
573
		// Cancel setting undo break point
573
		// Cancel setting undo break point
574
		[NSObject cancelPreviousPerformRequestsWithTarget:self 
574
		[NSObject cancelPreviousPerformRequestsWithTarget:self 
575
								selector:@selector(breakUndoCoalescing) 
575
								selector:@selector(breakUndoCoalescing) 
576
								object:nil];
576
								object:nil];
577
 
577
 
578
		// Improve undo behaviour, i.e. it depends how fast the user types
578
		// Improve undo behaviour, i.e. it depends how fast the user types
579
		[self performSelector:@selector(breakUndoCoalescing) withObject:nil afterDelay:0.8];
579
		[self performSelector:@selector(breakUndoCoalescing) withObject:nil afterDelay:0.8f];
580
 
580
 
581
	}
581
	}
582
 
582
 
583
	deleteBackward = NO;
583
	deleteBackward = NO;
584
	startListeningToBoundChanges = YES;
584
	startListeningToBoundChanges = YES;
Line 667... Line 667...
667
	isSyntaxHighlighting = YES;
667
	isSyntaxHighlighting = YES;
668
 
668
 
669
	NSString *selfstr    = [theTextStorage string];
669
	NSString *selfstr    = [theTextStorage string];
670
	NSInteger strlength  = (NSInteger)[selfstr length];
670
	NSInteger strlength  = (NSInteger)[selfstr length];
671
 
671
 
672
 
-
 
673
	// do not highlight if text larger than 10MB
672
	// do not highlight if text larger than 10MB
674
	if(strlength > 10000000) return;
673
	if(strlength > 10000000 || !strlength) return;
675
 
674
 
676
	// == Do highlighting partly (max R_SYNTAX_HILITE_BIAS*2 around visibleRange
675
	// == Do highlighting partly (max R_SYNTAX_HILITE_BIAS*2 around visibleRange
677
	// by considering entire lines).
676
	// by considering entire lines).
678
 
677
 
679
	// Get the text range currently displayed in the view port
678
	// Get the text range currently displayed in the view port
Line 704... Line 703...
704
		}
703
		}
705
	}
704
	}
706
 
705
 
707
	NSRange textRange = NSMakeRange(start, end-start);
706
	NSRange textRange = NSMakeRange(start, end-start);
708
 
707
 
709
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
-
 
710
	[[self layoutManager] ensureLayoutForCharacterRange:textRange];
-
 
711
#endif
-
 
712
 
-
 
713
	// only to be sure that nothing went wrongly
708
	// only to be sure that nothing went wrongly
714
	textRange = NSIntersectionRange(textRange, NSMakeRange(0, [theTextStorage length])); 
709
	textRange = NSIntersectionRange(textRange, NSMakeRange(0, [theTextStorage length])); 
715
 
710
 
716
	if (!textRange.length) {
711
	if (!textRange.length) {
717
		isSyntaxHighlighting = NO;
712
		isSyntaxHighlighting = NO;
Line 723... Line 718...
723
	NSColor *tokenColor = nil;
718
	NSColor *tokenColor = nil;
724
 
719
 
725
	size_t token;
720
	size_t token;
726
	NSRange tokenRange;
721
	NSRange tokenRange;
727
 
722
 
728
	// first remove the old colors and kQuote
-
 
729
	// [theTextStorage removeAttribute:NSForegroundColorAttributeName range:textRange];
-
 
730
	// mainly for suppressing auto-pairing in 
-
 
731
	// [theTextStorage removeAttribute:kLEXToken range:textRange];
-
 
732
 
-
 
733
	// initialise flex
723
	// initialise flex
734
	yyuoffset = textRange.location; yyuleng = 0;
724
	yyuoffset = textRange.location; yyuleng = 0;
735
 
725
 
736
	if([[self delegate] isRdDocument]) {
726
	if([[self delegate] isRdDocument]) {
737
 
727
 
738
			rd_switch_to_buffer(rd_scan_string(NSStringUTF8String([selfstr substringWithRange:textRange])));
728
			rd_switch_to_buffer(rd_scan_string(NSStringUTF8String([selfstr substringWithRange:textRange])));
739
 
729
 
740
			// now loop through all the tokens
730
			// now loop through all the tokens
741
			while ((token = rdlex())) {
731
			while ((token = rdlex())) {
742
		// NSLog(@"t %d", token);
-
 
743
				switch (token) {
732
				switch (token) {
744
					case RDPT_COMMENT:
733
					case RDPT_COMMENT:
745
					    tokenColor = shColorComment;
734
					    tokenColor = shColorComment;
746
					    break;
735
					    break;
747
					case RDPT_SECTION:
736
					case RDPT_SECTION:
Line 771... Line 760...
771
				tokenRange = NSIntersectionRange(tokenRange, textRange);
760
				tokenRange = NSIntersectionRange(tokenRange, textRange);
772
				if (!tokenRange.length) continue;
761
				if (!tokenRange.length) continue;
773
 
762
 
774
				NSMutableAttributedStringAddAttributeValueRange(theTextStorage, NSForegroundColorAttributeName, tokenColor, tokenRange);
763
				NSMutableAttributedStringAddAttributeValueRange(theTextStorage, NSForegroundColorAttributeName, tokenColor, tokenRange);
775
 
764
 
776
				// Add an attribute to be used in the auto-pairing (keyDown:)
-
 
777
				// to disable auto-pairing if caret is inside of any token found by lex.
-
 
778
				// For discussion: maybe change it later (only for quotes not keywords?)
-
 
779
				// if(token < 6)
-
 
780
				// 	NSMutableAttributedStringAddAttributeValueRange(theTextStorage, kLEXToken, kLEXTokenValue, tokenRange);
-
 
781
 
-
 
782
 
-
 
783
				// Add an attribute to be used to distinguish quotes from keywords etc.
-
 
784
				// used e.g. in completion suggestions
-
 
785
				// else if(token < 4)
-
 
786
				// 	NSMutableAttributedStringAddAttributeValueRange(theTextStorage, kQuote, kQuoteValue, tokenRange);
-
 
787
 
-
 
788
			}
765
			}
789
 
766
 
790
	} else {
767
	} else {
791
 
768
 
792
		yy_switch_to_buffer(yy_scan_string(NSStringUTF8String([selfstr substringWithRange:textRange])));
769
		yy_switch_to_buffer(yy_scan_string(NSStringUTF8String([selfstr substringWithRange:textRange])));
793
 
770
 
794
		// now loop through all the tokens
771
		// now loop through all the tokens
795
		while ((token = yylex())) {
772
		while ((token = yylex())) {
796
	// NSLog(@"t %d", token);
-
 
797
			switch (token) {
773
			switch (token) {
798
				case RPT_SINGLE_QUOTED_TEXT:
774
				case RPT_SINGLE_QUOTED_TEXT:
799
				case RPT_DOUBLE_QUOTED_TEXT:
775
				case RPT_DOUBLE_QUOTED_TEXT:
800
				    tokenColor = shColorString;
776
				    tokenColor = shColorString;
801
				    break;
777
				    break;
Line 812... Line 788...
812
				    tokenColor = shColorComment;
788
				    tokenColor = shColorComment;
813
				    break;
789
				    break;
814
				case RPT_VARIABLE:
790
				case RPT_VARIABLE:
815
				    tokenColor = shColorIdentifier;
791
				    tokenColor = shColorIdentifier;
816
				    break;
792
				    break;
817
				case RPT_WHITESPACE:
-
 
818
				    continue;
-
 
819
				    break;
-
 
820
				default:
793
				default:
821
				    tokenColor = shColorNormal;
794
				    tokenColor = shColorNormal;
822
			}
795
			}
823
 
796
 
824
			tokenRange = NSMakeRange(yyuoffset, yyuleng);
797
			tokenRange = NSMakeRange(yyuoffset, yyuleng);
Line 829... Line 802...
829
			tokenRange = NSIntersectionRange(tokenRange, textRange);
802
			tokenRange = NSIntersectionRange(tokenRange, textRange);
830
			if (!tokenRange.length) continue;
803
			if (!tokenRange.length) continue;
831
 
804
 
832
			NSMutableAttributedStringAddAttributeValueRange(theTextStorage, NSForegroundColorAttributeName, tokenColor, tokenRange);
805
			NSMutableAttributedStringAddAttributeValueRange(theTextStorage, NSForegroundColorAttributeName, tokenColor, tokenRange);
833
		
806
		
834
			// Add an attribute to be used in the auto-pairing (keyDown:)
-
 
835
			// to disable auto-pairing if caret is inside of any token found by lex.
-
 
836
			// For discussion: maybe change it later (only for quotes not keywords?)
-
 
837
			// if(token < 6)
-
 
838
			// 	NSMutableAttributedStringAddAttributeValueRange(theTextStorage, kLEXToken, kLEXTokenValue, tokenRange);
-
 
839
		
-
 
840
 
-
 
841
			// Add an attribute to be used to distinguish quotes from keywords etc.
-
 
842
			// used e.g. in completion suggestions
-
 
843
			// if(token < 4)
-
 
844
			// 	NSMutableAttributedStringAddAttributeValueRange(theTextStorage, kQuote, kQuoteValue, tokenRange);
-
 
845
		
-
 
846
 
-
 
847
		}
807
		}
848
	}
808
	}
849
 
809
 
850
	// set current textColor to the color of the caret's position - 1
810
	// set current textColor to the color of the caret's position - 1
851
	// to try to suppress writing in normalColor before syntax highlighting 
811
	// to try to suppress writing in normalColor before syntax highlighting 
Line 859... Line 819...
859
	}
819
	}
860
 
820
 
861
	[theTextStorage endEditing];
821
	[theTextStorage endEditing];
862
	isSyntaxHighlighting = NO;
822
	isSyntaxHighlighting = NO;
863
 
823
 
864
	[self setNeedsDisplay:YES];
824
	[self setNeedsDisplayInRect:[self bounds]];
865
 
825
 
866
}
826
}
867
 
827
 
868
-(void)resetHighlights
828
-(void)resetHighlights
869
{
829
{