Join/Login

Business Software

Open Source Software

For Vendors

Blog

About

More

Articles

Create

SourceForge Podcast

Site Documentation

Subscribe to our Newsletter

Support Request

For Vendors Help Create Join Login

Business Software

Open Source Software

SourceForge Podcast

Resources

Articles

Case Studies

Blog

Menu

Help

Create

Join

Login

Home

Browse

ODBCKit

Code

ODBCKit Code

Status: Beta
Brought to you by: hawkmoon

Summary

Files

Reviews

Support

Mailing Lists

Tickets ▾

Bugs

Support Requests

Feature Requests

News

Discussion

Donate

Code

Menu ▾ ▴

Browse Commits

Browse Files

[r93]: / ODBCQueryTool / EditorDelegate.m  Maximize  Restore  History

Download this file

231 lines (184 with data), 6.5 kB

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

//

// EditorDelegate.m

// Query Tool for Postgres

//

// Created by Andy Satori on 9/25/06.

// Copyright 2006 Druware Software Designs. All rights reserved.

//

// SQL Comments:

// -- single line

// # single line (MySQL)

// /* */ multi-line

// SQL Literals:

// ' string

// " string

// [] quoted identifier

#import "EditorDelegate.h"

@implementation MyDocument (MyDocumentEditor)

#pragma mark Utility Functions

#pragma mark Syntax Match Functions

- (BOOL)isValueTablename:(NSString *)value

{

if (schemaData == nil) { return NO; }

if ([schemaData tableNames] == nil) { return NO; }

// return NO;

int x;

NSString *trimmedValue = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

for (x = 0; x < [[schemaData tableNames] count]; x++)

{

NSString *tableName = (NSString *)[[schemaData tableNames] objectAtIndex:x];

if ([tableName caseInsensitiveCompare:trimmedValue] == NSOrderedSame)

{

return YES;

}

}

return NO;

}

- (BOOL)isValueKeyword:(NSString *)value

{

int x;

NSString *trimmedValue = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

for (x = 0; x < [keywords count]; x++)

{

NSString *keyword = (NSString *)[keywords objectAtIndex:x];

if ([keyword caseInsensitiveCompare:trimmedValue] == NSOrderedSame)

{

return YES;

}

}

return NO;

}

- (BOOL)isCharacterDelimiter:(char)value

{

if (delimiters == nil)

{

delimiters = [[NSString alloc] initWithString:@" ,.![]();:'\"\t\r\n+=&<>/*-"];

[[delimiters retain] autorelease];

}

NSRange loc;

loc = [delimiters rangeOfString:[NSString stringWithFormat:@"%c", value]];

if (loc.location == NSNotFound)

{

return NO;

}

return YES;

}

- (BOOL)isCharacterNewLine:(char)value

{

NSString *lineDelimiters = [[NSString alloc] initWithString:@"\r\n"];

NSRange loc;

loc = [lineDelimiters rangeOfString:[NSString stringWithFormat:@"%c", value]];

if (loc.location == NSNotFound)

{

return NO;

}

return YES;

}

#pragma mark Syntax Parsing Elements

- (void)colorRange:(NSRange)rangeToColor

{

// loop through the range, breaking at each delimiter to set the attributes

long i;

i = rangeToColor.location;

NSRange rangeOfWord;

rangeOfWord.location = rangeToColor.location;

for (i = rangeToColor.location; i < (rangeToColor.location + rangeToColor.length); i++)

{

// break on delimiters

if ([self isCharacterDelimiter:[[query string] characterAtIndex:i]]) // needs to be altered to 'delimiter'

{

rangeOfWord.length = i - rangeOfWord.location;

[self setAttributesForWord:rangeOfWord];

rangeOfWord.location = i;

rangeOfWord.length = 0;

}

}

rangeOfWord.length = i - rangeOfWord.location;

[self setAttributesForWord:rangeOfWord];

}

- (void)setAttributesForWord:(NSRange)rangeOfCurrentWord

{

// set the attributes of the string

NSTextStorage *ts = [query textStorage];

// keywords

// NSColor *keywordColor = [NSColor colorWithCalibratedRed: 0.2 green: 0.2 blue: 1.0 alpha: 1.0];

NSDictionary *keywordAtts = [NSDictionary dictionaryWithObject:keywordColor

forKey:NSForegroundColorAttributeName];

//NSColor *tableColor = [NSColor colorWithCalibratedRed: 1.0 green: 0.2 blue: 1.0 alpha: 1.0];

NSDictionary *tableAtts = [NSDictionary dictionaryWithObject:tableNameColor

forKey:NSForegroundColorAttributeName];

// color the word

[[query layoutManager] removeTemporaryAttribute:NSForegroundColorAttributeName forCharacterRange:rangeOfCurrentWord];

if ([self isValueKeyword:[[ts attributedSubstringFromRange:rangeOfCurrentWord] string]])

{

[[query layoutManager] setTemporaryAttributes:keywordAtts forCharacterRange:rangeOfCurrentWord];

}

if ([self isValueTablename:[[ts attributedSubstringFromRange:rangeOfCurrentWord] string]])

{

[[query layoutManager] setTemporaryAttributes:tableAtts forCharacterRange:rangeOfCurrentWord];

}

}

# pragma mark Edit View Notification Handlers

- (void)textViewDidChangeSelection:(NSNotification *)aNotification

{

/* scanning logic is as follows:

* 1. is the current input text part of a comment

* 2. is the current input text part of a string literal

* 3. is the current input text a keyword

* 4. is the current input text a table name or active field name

* color it accordingly

*/

// based upon the current locatiodn, scan forward and backward to the nearest

// delimiter and highlight the current word based upon that delimiter

NSTextStorage *ts = [query textStorage];

NSRange rangeOfEdit = [ts editedRange];

NSRange rangeToColor = [ts editedRange];

// validate the range and exit if the range makes no sense

if (rangeOfEdit.location == NSNotFound)

{

return;

}

if (rangeOfEdit.length == 0) {

return;

}

// note: this particular sanity check may have performance concerns if the

// document is very long and the user selects all. it needs to be tested

// as the colorRange implementation expands.

if (rangeOfEdit.length > 1) {

[self colorRange:rangeOfEdit];

return;

}

// the range of the edit is just 1 character. unless that character is in

// our delimiter set then we really don't care and should not do any coloring

// our delimiter characters are tab, cr, lf, (, ), [, ], ;, ", ', space

long i = rangeOfEdit.location;

if (![self isCharacterDelimiter:[[ts string] characterAtIndex:i]])

{

// do not do anything on non-delimiter chars.

return;

}

// if the character is in our delimiter set then we need to do some processing

// to determine the appropriate range to select and color before we call

// colorRange on it.

// for the moment, we will simply scan to the beginning of the current line.

if (i >= [[ts string] length]) { i = [[ts string] length] - 1; }

i--;

if (i < 0) { return; }

char currentCharacter = [[ts string] characterAtIndex:i];

while ([self isCharacterNewLine:currentCharacter] == NO)

{

if (i < 0) { break; }

currentCharacter = [[ts string] characterAtIndex:i];

i--;

}

if (i < 0) { i = 0; }

// if the last item in the range is a '*/' then this is the close of a

// multi-line comment, and the rangeToColor needs to go back to the opening

// '/*' to do a proper coloring.

rangeToColor.location = i;

rangeToColor.length = rangeOfEdit.location - rangeToColor.location;

// if the edited range contains no delimiters...

[self colorRange:rangeToColor];

[self updateChangeCount:NSChangeDone];

}

@end

SourceForge

Create a Project

Open Source Software

Business Software

Top Downloaded Projects

Company

About

Team

SourceForge Headquarters
225 Broadway Suite 1600
San Diego, CA 92101
+1 (858) 422-6466

Resources

Support

Site Documentation

Site Status

SourceForge Reviews

© 2025 Slashdot Media. All Rights Reserved.

Terms Privacy Opt Out Advertise

×
Want the latest updates on software, tech news, and AI?

Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.

Submit