Moved the (deprecated) highlighting code into PCEditorView+Highlighting and

implemented tab (\t) capabilities.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/apps/projectcenter/trunk@12552 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Philippe C.D. Robert 2002-02-17 17:40:24 +00:00
parent bae1bafa98
commit 91c6d95415
7 changed files with 425 additions and 284 deletions

View file

@ -67,7 +67,8 @@ PCDefines.h \
PCTextFinder.h \
PCProjectEditor.h \
ProjectComponent.h \
PCProject+ComponentHandling.h
PCProject+ComponentHandling.h \
PCEditorView+Highlighting.h
#
@ -90,7 +91,8 @@ PCEditor.m \
PCEditorController.m \
PCTextFinder.m \
PCProjectEditor.m \
PCProject+ComponentHandling.m
PCProject+ComponentHandling.m \
PCEditorView+Highlighting.m
#

View file

@ -0,0 +1,44 @@
/*
GNUstep ProjectCenter - http://www.gnustep.org
Copyright (C) 2001 Free Software Foundation
Author: Philippe C.D. Robert <phr@3dkit.org>
This file is part of GNUstep.
This application is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
$Id$
*/
#ifndef _PCEDITORVIEW_HIGHLIGHTING_H
#define _PCEDITORVIEW_HIGHLIGHTING_H
#import <ProjectCenter/PCEditorView.h>
@interface PCEditorView (Highlighting)
- (void)colouriseKeyword:(NSString *)keyword;
- (void)colouriseKeywords:(NSArray *)keywords;
- (void)colouriseStrings;
- (void)colouriseCharStrings;
- (void)colouriseComments;
- (void)colouriseCPPComments;
@end
#endif

View file

@ -0,0 +1,285 @@
/*
GNUstep ProjectCenter - http://www.gnustep.org
Copyright (C) 2001 Free Software Foundation
Author: Philippe C.D. Robert <phr@3dkit.org>
This file is part of GNUstep.
This application is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
$Id$
*/
#import "PCEditorView+Highlighting.h"
#define SCANLOC [scanner scanLocation]
@implementation PCEditorView (Highlighting)
static NSColor *commentColor = nil;
static NSColor *keywordColor = nil;
static NSColor *cppCommentColor = nil;
static NSColor *stringColor = nil;
static NSColor *cStringColor = nil;
static NSFont *editorFont = nil;
+ (void)initialize
{
static BOOL initialised = NO;
if( !initialised )
{
initialised = YES;
#define CALIBRATED_COL(r, g, b, a) RETAIN([NSColor colorWithCalibratedRed:r green:g blue:b alpha:a]);
commentColor = CALIBRATED_COL(0.0,0.5,0.0,1.0);
keywordColor = CALIBRATED_COL(0.0,0.5,0.0,1.0);
cppCommentColor = CALIBRATED_COL(0.8,0.0,0.0,1.0);
stringColor = CALIBRATED_COL(0.0,0.0,0.8,1.0);
cStringColor = CALIBRATED_COL(0.0,0.0,0.8,1.0);
editorFont = RETAIN([NSFont userFixedPitchFontOfSize:12]);
#undef CALIBRATED_COL
}
[super initialize];
}
- (void)colouriseStrings
{
BOOL foundRange;
id aDict = [NSDictionary dictionaryWithObjectsAndKeys:
stringColor, NSForegroundColorAttributeName,
@"StringConstantCodeType", @"PCCodeTypeAttributeName", nil ];
[scanner setScanLocation:0];
while( ![scanner isAtEnd] ) {
foundRange = NO;
[scanner scanUpToString:@"\"" intoString:NULL];
range.location = SCANLOC;
[scanner scanString:@"\"" intoString:NULL];
if( ![scanner isAtEnd] &&
range.location > 0 &&
[[_textStorage string] characterAtIndex:(SCANLOC - 2)] == '@' ) {
range.location -= 1;
}
while( ![scanner isAtEnd] ) {
[scanner scanUpToString:@"\"" intoString:NULL];
[scanner scanString:@"\"" intoString:NULL];
// If there is no escape char before then we are done..
if( [[scanner string] characterAtIndex:(SCANLOC - 2)] != '\\' ||
[[scanner string] characterAtIndex:(SCANLOC - 3)] == '\\' ) {
range.length = SCANLOC - range.location;
foundRange = YES;
break;
}
}
if( foundRange ) {
NS_DURING
[_textStorage addAttributes:aDict range:range];
NS_HANDLER
NSLog(@"<%@ %x> raised (-scanForStrings):\n%@",[self class],self,[localException description]);
NS_ENDHANDLER
}
}
}
- (void)colouriseCharStrings
{
NSRange tmpRange;
BOOL foundRange;
id aDict = [NSDictionary dictionaryWithObjectsAndKeys:
cStringColor,NSForegroundColorAttributeName,
@"StringConstantCodeType", @"PCCodeTypeAttributeName", nil ];
[scanner setScanLocation:0];
while( ![scanner isAtEnd] ) {
foundRange = NO;
[scanner scanUpToString:@"'" intoString:NULL];
range.location = SCANLOC;
[scanner scanString:@"'" intoString:NULL];
while( ![scanner isAtEnd] ) {
[scanner scanUpToString:@"'" intoString:NULL];
[scanner scanString:@"'" intoString:NULL];
// No escape => we are done! (ugly hack...)
if( [[scanner string] characterAtIndex:(SCANLOC - 2)] != '\\' ||
[[scanner string] characterAtIndex:(SCANLOC - 3)] == '\\' ) {
range.length = SCANLOC - range.location;
// Ranges are not longer than 8 chars! (ugly hack...)
if( range.length > 8 ) {
[scanner setScanLocation:SCANLOC - 1];
}
else {
foundRange = YES;
}
break;
}
}
if( foundRange ) {
NS_DURING
[_textStorage addAttributes:aDict range:range];
NS_HANDLER
NSLog(@"<%@ %x> raised (-colouriseCharStrings):\n%@",[self class],self,[localException description]);
NS_ENDHANDLER
}
}
}
- (void)colouriseComments
{
NSRange tmpRange;
BOOL foundRange;
id anObject;
id aDict = [NSDictionary dictionaryWithObjectsAndKeys:
commentColor,NSForegroundColorAttributeName,
@"CommentCodeType", @"PCCodeTypeAttributeName",
nil ];
[scanner setScanLocation:0];
while( ![scanner isAtEnd] ) {
foundRange = NO;
while( ![scanner isAtEnd] ) {
[scanner scanUpToString:@"/*" intoString:NULL];
range.location = SCANLOC;
[scanner scanString:@"/*" intoString:NULL];
if(![scanner isAtEnd] &&
[[_textStorage attribute:@"PCCodeTypeAttributeName"
atIndex:range.location
effectiveRange:&tmpRange] isEqual:@"UnknownCodeType"]){
foundRange = YES;
break;
}
}
[scanner scanUpToString:@"*/" intoString:NULL];
[scanner scanString:@"*/" intoString:NULL];
range.length = SCANLOC - range.location;
if( foundRange ) {
NS_DURING
/*
* BIG HACK!!!
*/
if (range.location == 0) {range.location = 1;range.length--;}
[_textStorage addAttributes:aDict range:range];
NS_HANDLER
NSLog(@"<%@ %x> raised (-colouriseComments):\n%@",[self class],self,[localException description]);
NS_ENDHANDLER
}
}
}
- (void)colouriseCPPComments
{
NSRange tmpRange;
BOOL foundRange;
id anObject;
id aDict = [NSDictionary dictionaryWithObjectsAndKeys:
cppCommentColor, NSForegroundColorAttributeName,
@"CommentCodeType", @"PCCodeTypeAttributeName", nil ];
[scanner setScanLocation:0];
while( ![scanner isAtEnd] ) {
foundRange = NO;
while( ![scanner isAtEnd] ) {
[scanner scanUpToString:@"//" intoString:NULL];
range.location = SCANLOC;
[scanner scanString:@"//" intoString:NULL];
if( ![scanner isAtEnd] &&
[[_textStorage attribute:@"PCCodeTypeAttributeName"
atIndex:range.location
effectiveRange:&tmpRange] isEqual:@"UnknownCodeType"]){
foundRange = YES;
break;
}
}
[scanner scanUpToString:@"\n" intoString:NULL];
[scanner scanString:@"\n" intoString:NULL];
range.length = SCANLOC - range.location;
if( foundRange ) {
NS_DURING
[_textStorage addAttributes:aDict range:range];
NS_HANDLER
NSLog(@"<%@ %x> raised (-colouriseCPPComments):\n%@",[self class],self,[localException description]);
NS_ENDHANDLER
}
}
}
- (void)colouriseKeyword:(NSString *)keyword
{
NSRange tmpRange;
BOOL foundRange;
id anObject;
id keywordDict = [NSDictionary dictionaryWithObjectsAndKeys:
keywordColor,NSForegroundColorAttributeName,
@"KeywordCodeType", @"PCCodeTypeAttributeName", nil ];
// First scan for docu style comments
[scanner setScanLocation:0];
while( ![scanner isAtEnd] ) {
[scanner scanUpToString:keyword intoString:NULL];
range.location = SCANLOC;
if( ![scanner isAtEnd] &&
[[_textStorage attribute:@"PCCodeTypeAttributeName"
atIndex:range.location
effectiveRange:&tmpRange] isEqual:@"UnknownCodeType"] ) {
NS_DURING
[_textStorage addAttributes:keywordDict range:NSMakeRange( range.location, [keyword length])];
NS_HANDLER
NSLog(@"<%@ %x> raised (-colouriseKeyword:):\n%@",[self class],self,[localException description]);
NS_ENDHANDLER
}
[scanner scanString:keyword intoString:NULL];
}
}
- (void)colouriseKeywords:(NSArray *)keywords
{
NSEnumerator *enumerator = [keywords objectEnumerator];
id object;
while ((object = [enumerator nextObject])) {
[self colouriseKeyword:object];
}
}
@end

View file

@ -24,10 +24,20 @@
$Id$
*/
#ifndef _PCEDITORVIEW_H
#define _PCEDITORVIEW_H
#import <AppKit/AppKit.h>
@class PCEditor;
typedef enum _PCTabFlags {
PCTabTab = 1,
PCTab2Sp,
PCTab4Sp,
PCTab8Sp
} PCTabFlags;
@interface PCEditorView : NSTextView
{
NSScanner *scanner;
@ -36,33 +46,45 @@
NSRange range;
NSArray *_keywords;
PCEditor *editor;
BOOL shouldHighlight;
}
//=============================================================================
// ==== Class methods
//=============================================================================
+ (void)setTabBehaviour:(int)tabFlags;
+ (int)tabBehaviour;
+ (void)setShouldHighlight:(BOOL)yn;
+ (BOOL)shouldHighlight;
//=============================================================================
// ==== Init
//=============================================================================
- (id)initWithFrame:(NSRect)frameRect textContainer:(NSTextContainer*)tc;
- (void)dealloc;
//=============================================================================
// ==== Accessor methods
//=============================================================================
- (void)setEditor:(PCEditor *)anEditor;
- (void)setString:(NSString *)aString;
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent;
//=============================================================================
// ==== Text handling
//=============================================================================
- (void)setShouldHighlight:(BOOL)yn;
- (BOOL)shouldHighlight;
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent;
- (void)insertText:(id)aString;
- (void)highlightText;
- (void)highlightTextInRange:(NSRange)range;
- (void)colouriseKeyword:(NSString *)keyword;
- (void)colouriseKeywords:(NSArray *)keywords;
- (void)colouriseStrings;
- (void)colouriseCharStrings;
- (void)colouriseComments;
- (void)colouriseCPPComments;
- (void)keyDown: (NSEvent *)anEvent;
@end
#endif

View file

@ -26,19 +26,32 @@
#import "PCEditorView.h"
#import "PCEditor.h"
#define COLOURISE 0
#define SCANLOC [scanner scanLocation]
#import "PCEditorView+Highlighting.h"
@implementation PCEditorView
static NSColor *commentColor = nil;
static NSColor *keywordColor = nil;
static NSColor *cppCommentColor = nil;
static NSColor *stringColor = nil;
static NSColor *cStringColor = nil;
static NSFont *editorFont = nil;
static BOOL isInitialised = NO;
static BOOL shouldHighlight = NO;
static int _tabFlags = PCTab4Sp;
+ (void)setTabBehaviour:(int)tabFlags
{
_tabFlags = tabFlags;
}
+ (int)tabBehaviour
{
return _tabFlags;
}
+ (void)setShouldHighlight:(BOOL)yn
{
shouldHighlight = yn;
}
+ (BOOL)shouldHighlight
{
return shouldHighlight;
}
- (id)initWithFrame:(NSRect)frameRect textContainer:(NSTextContainer*)tc
{
@ -46,20 +59,6 @@ static BOOL isInitialised = NO;
{
shouldHighlight = NO;
/*
* Should move that to initialize...
*/
if (isInitialised == NO) {
commentColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.5 blue: 0.0 alpha: 1.0] retain];
cppCommentColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.5 blue: 0.0 alpha: 1.0] retain];
keywordColor = [[NSColor colorWithCalibratedRed: 0.8 green: 0.0 blue: 0.0 alpha: 1.0] retain];
stringColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.0 blue: 0.8 alpha: 1.0] retain];
cStringColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.0 blue: 0.8 alpha: 1.0] retain];
editorFont = [[NSFont userFixedPitchFontOfSize:12] retain];
isInitialised = YES;
}
_keywords = [[NSArray alloc] initWithObjects:@"@class",
@"@selector",
@"#import",
@ -101,12 +100,11 @@ static BOOL isInitialised = NO;
scanner = [[NSScanner alloc] initWithString:aString];
[super setString:aString];
#ifdef COLOURISE
if( shouldHighlight )
{
[self highlightText];
}
#endif //COLOURISE
}
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
@ -114,16 +112,6 @@ static BOOL isInitialised = NO;
return YES;
}
- (void)setShouldHighlight:(BOOL)yn
{
shouldHighlight = yn;
}
- (BOOL)shouldHighlight
{
return shouldHighlight;
}
- (void)insertText:(id)aString
{
NSRange txtRange = NSMakeRange(0, [[self textStorage] length]);
@ -146,16 +134,18 @@ static BOOL isInitialised = NO;
- (void)highlightTextInRange:(NSRange)txtRange
{
NSDictionary *aDict;
//NSDictionary *aDict;
NSArray *keywords;
/*
aDict = [NSDictionary dictionaryWithObjectsAndKeys:
editorFont, NSFontAttributeName,
@"UnknownCodeType", @"PCCodeTypeAttributeName",
nil];
*/
[_textStorage beginEditing];
[_textStorage setAttributes:aDict range:txtRange];
[_textStorage setAttributes:nil range:txtRange];
// Scan the CodeType first...
@ -178,230 +168,6 @@ static BOOL isInitialised = NO;
[self setNeedsDisplay:YES];
}
- (void)colouriseStrings
{
BOOL foundRange;
id aDict = [NSDictionary dictionaryWithObjectsAndKeys:
stringColor, NSForegroundColorAttributeName,
@"StringConstantCodeType", @"PCCodeTypeAttributeName", nil ];
[scanner setScanLocation:0];
while( ![scanner isAtEnd] ) {
foundRange = NO;
[scanner scanUpToString:@"\"" intoString:NULL];
range.location = SCANLOC;
[scanner scanString:@"\"" intoString:NULL];
if( ![scanner isAtEnd] &&
range.location > 0 &&
[[_textStorage string] characterAtIndex:(SCANLOC - 2)] == '@' ) {
range.location -= 1;
}
while( ![scanner isAtEnd] ) {
[scanner scanUpToString:@"\"" intoString:NULL];
[scanner scanString:@"\"" intoString:NULL];
// If there is no escape char before then we are done..
if( [[scanner string] characterAtIndex:(SCANLOC - 2)] != '\\' ||
[[scanner string] characterAtIndex:(SCANLOC - 3)] == '\\' ) {
range.length = SCANLOC - range.location;
foundRange = YES;
break;
}
}
if( foundRange ) {
NS_DURING
[_textStorage addAttributes:aDict range:range];
NS_HANDLER
NSLog(@"<%@ %x> raised (-scanForStrings):\n%@",[self class],self,[localException description]);
NS_ENDHANDLER
}
}
}
- (void)colouriseCharStrings
{
NSRange tmpRange;
BOOL foundRange;
id aDict = [NSDictionary dictionaryWithObjectsAndKeys:
cStringColor,NSForegroundColorAttributeName,
@"StringConstantCodeType", @"PCCodeTypeAttributeName", nil ];
[scanner setScanLocation:0];
while( ![scanner isAtEnd] ) {
foundRange = NO;
[scanner scanUpToString:@"'" intoString:NULL];
range.location = SCANLOC;
[scanner scanString:@"'" intoString:NULL];
while( ![scanner isAtEnd] ) {
[scanner scanUpToString:@"'" intoString:NULL];
[scanner scanString:@"'" intoString:NULL];
// No escape => we are done! (ugly hack...)
if( [[scanner string] characterAtIndex:(SCANLOC - 2)] != '\\' ||
[[scanner string] characterAtIndex:(SCANLOC - 3)] == '\\' ) {
range.length = SCANLOC - range.location;
// Ranges are not longer than 8 chars! (ugly hack...)
if( range.length > 8 ) {
[scanner setScanLocation:SCANLOC - 1];
}
else {
foundRange = YES;
}
break;
}
}
if( foundRange ) {
NS_DURING
[_textStorage addAttributes:aDict range:range];
NS_HANDLER
NSLog(@"<%@ %x> raised (-colouriseCharStrings):\n%@",[self class],self,[localException description]);
NS_ENDHANDLER
}
}
}
- (void)colouriseComments
{
NSRange tmpRange;
BOOL foundRange;
id anObject;
id aDict = [NSDictionary dictionaryWithObjectsAndKeys:
commentColor,NSForegroundColorAttributeName,
@"CommentCodeType", @"PCCodeTypeAttributeName",
nil ];
[scanner setScanLocation:0];
while( ![scanner isAtEnd] ) {
foundRange = NO;
while( ![scanner isAtEnd] ) {
[scanner scanUpToString:@"/*" intoString:NULL];
range.location = SCANLOC;
[scanner scanString:@"/*" intoString:NULL];
if(![scanner isAtEnd] &&
[[_textStorage attribute:@"PCCodeTypeAttributeName"
atIndex:range.location
effectiveRange:&tmpRange] isEqual:@"UnknownCodeType"]){
foundRange = YES;
break;
}
}
[scanner scanUpToString:@"*/" intoString:NULL];
[scanner scanString:@"*/" intoString:NULL];
range.length = SCANLOC - range.location;
if( foundRange ) {
NS_DURING
/*
* BIG HACK!!!
*/
if (range.location == 0) {range.location = 1;range.length--;}
[_textStorage addAttributes:aDict range:range];
NS_HANDLER
NSLog(@"<%@ %x> raised (-colouriseComments):\n%@",[self class],self,[localException description]);
NS_ENDHANDLER
}
}
}
- (void)colouriseCPPComments
{
NSRange tmpRange;
BOOL foundRange;
id anObject;
id aDict = [NSDictionary dictionaryWithObjectsAndKeys:
cppCommentColor, NSForegroundColorAttributeName,
@"CommentCodeType", @"PCCodeTypeAttributeName", nil ];
[scanner setScanLocation:0];
while( ![scanner isAtEnd] ) {
foundRange = NO;
while( ![scanner isAtEnd] ) {
[scanner scanUpToString:@"//" intoString:NULL];
range.location = SCANLOC;
[scanner scanString:@"//" intoString:NULL];
if( ![scanner isAtEnd] &&
[[_textStorage attribute:@"PCCodeTypeAttributeName"
atIndex:range.location
effectiveRange:&tmpRange] isEqual:@"UnknownCodeType"]){
foundRange = YES;
break;
}
}
[scanner scanUpToString:@"\n" intoString:NULL];
[scanner scanString:@"\n" intoString:NULL];
range.length = SCANLOC - range.location;
if( foundRange ) {
NS_DURING
[_textStorage addAttributes:aDict range:range];
NS_HANDLER
NSLog(@"<%@ %x> raised (-colouriseCPPComments):\n%@",[self class],self,[localException description]);
NS_ENDHANDLER
}
}
}
- (void)colouriseKeyword:(NSString *)keyword
{
NSRange tmpRange;
BOOL foundRange;
id anObject;
id keywordDict = [NSDictionary dictionaryWithObjectsAndKeys:
keywordColor,NSForegroundColorAttributeName,
@"KeywordCodeType", @"PCCodeTypeAttributeName", nil ];
// First scan for docu style comments
[scanner setScanLocation:0];
while( ![scanner isAtEnd] ) {
[scanner scanUpToString:keyword intoString:NULL];
range.location = SCANLOC;
if( ![scanner isAtEnd] &&
[[_textStorage attribute:@"PCCodeTypeAttributeName"
atIndex:range.location
effectiveRange:&tmpRange] isEqual:@"UnknownCodeType"] ) {
NS_DURING
[_textStorage addAttributes:keywordDict range:NSMakeRange( range.location, [keyword length])];
NS_HANDLER
NSLog(@"<%@ %x> raised (-colouriseKeyword:):\n%@",[self class],self,[localException description]);
NS_ENDHANDLER
}
[scanner scanString:keyword intoString:NULL];
}
}
- (void)colouriseKeywords:(NSArray *)keywords
{
NSEnumerator *enumerator = [keywords objectEnumerator];
id object;
while ((object = [enumerator nextObject])) {
[self colouriseKeyword:object];
}
}
- (void)keyDown:(NSEvent *)anEvent
{
/*
@ -415,11 +181,30 @@ static BOOL isInitialised = NO;
}
*/
[super keyDown:anEvent];
if( ![[anEvent characters] compare: @"\t"] )
{
switch( _tabFlags )
{
case PCTabTab:
[self insertText: @"\t"];
break;
case PCTab2Sp:
[self insertText: @" "];
break;
case PCTab4Sp:
[self insertText: @" "];
break;
case PCTab8Sp:
[self insertText: @" "];
break;
default:
break;
}
}
else
{
[super keyDown:anEvent];
}
}
@end

View file

@ -50,6 +50,7 @@
#import <ProjectCenter/PCEditor.h>
#import <ProjectCenter/PCEditorController.h>
#import <ProjectCenter/PCEditorView.h>
#import <ProjectCenter/PCEditorView+Highlighting.h>
#import <ProjectCenter/PCTextFinder.h>
#endif

View file

@ -17,7 +17,8 @@
PCEditorController.m,
PCTextFinder.m,
PCProjectEditor.m,
PCProject+ComponentHandling.m
PCProject+ComponentHandling.m,
PCEditorView+Highlighting.m
);
COMPILEROPTIONS = "";
CREATION_DATE = "";
@ -52,7 +53,8 @@
PCTextFinder.h,
PCProjectEditor.h,
ProjectComponent.h,
PCProject+ComponentHandling.h
PCProject+ComponentHandling.h,
PCEditorView+Highlighting.h
);
INSTALLDIR = "$(GNUSTEP_SYSTEM_ROOT)";
LANGUAGE = English;