File: MSQLSyntaxColoring.m

package info (click to toggle)
mysql-admin 1.2.5rc-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 80,944 kB
  • ctags: 43,103
  • sloc: sql: 295,916; pascal: 256,535; cpp: 74,487; ansic: 68,881; objc: 26,417; sh: 16,867; yacc: 10,755; java: 9,917; xml: 8,453; php: 2,806; python: 2,068; makefile: 1,252; perl: 3
file content (125 lines) | stat: -rw-r--r-- 3,358 bytes parent folder | download | duplicates (4)
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
//
//  MSQLSyntaxColoring.m
//  MySQLGUICommon
//
//  Created by Alfredo Kojima on 5/20/05.
//  Copyright 2005 MySQL AB. All rights reserved.
//

#import "MSQLSyntaxColoring.h"


static NSString *tokenNames[]= {
  @"NormalColor",
  @"IdentifierColor",
  @"IdentifierColor",
  @"CommentColor",
  @"StringColor",
  @"SymbolColor",
  @"FunctionColor"
};

@implementation MSQLSyntaxColoring

- (id)initForTextView:(NSTextView*)textView
           syntaxInfo:(MYX_SYN*)info
{
  self= [super initForTextView:textView];
  if (self)
  {
    _syn= info;
    _hl= myx_init_sql_parsing(_syn);
  }
  return self;
}


- (void)dealloc
{
  myx_free_sql_highlighting(_hl);
  [super dealloc];
}


- (void)recolorRange:(NSRange)charRange
{
  NSRange lineRange= [self lineRangeForCharacterRange:charRange];

  if (lineRange.location != NSNotFound)
  {
    NSString *string= [[_textView textStorage] string];
    unsigned int stringLength= [string length];
    unsigned int line;
    
    for (line= lineRange.location; line <= NSMaxRange(lineRange); line++)
    {
      unsigned int i;
      NSRange range;

      range= [self characterRangeOfLine:line];
      
      [[_textView layoutManager] removeTemporaryAttribute:NSForegroundColorAttributeName
                                        forCharacterRange:range];
      
      myx_highlight(_hl, [[string substringWithRange:range] UTF8String], line);
      
      for (i= 0; i < _hl->words_num; i++)
      {
        NSRange wordRange;
        if (_hl->words[i].word_begin == _hl->words[i].word_end)
          continue;
        
        wordRange= NSMakeRange(_hl->words[i].word_begin + range.location,
                               MIN(_hl->words[i].word_end-_hl->words[i].word_begin+1, stringLength-_hl->words[i].word_begin));
        
        [[_textView layoutManager] addTemporaryAttributes:[_colors objectForKey:tokenNames[_hl->words[i].word_type]]
                                        forCharacterRange:wordRange];
      }
    }
  }
}


- (NSArray*)completionListForWord:(NSString*)word
{
  MYX_SYN_SUGGESTIONS *sugg= myx_lookup_word(_syn, [word UTF8String]);
  if (sugg)
  {
    unsigned int i;
    NSMutableArray *array= [NSMutableArray arrayWithCapacity:sugg->suggestions_num];
    
    for (i= 0; i < sugg->suggestions_num; i++)
    {
      if (strncasecmp(sugg->suggestions[i].name, [word UTF8String], [word length])==0)
      {
        if (sugg->suggestions[i].s_type == MYX_SYN_SYMBOL && [word canBeConvertedToEncoding:NSASCIIStringEncoding])
        {
          char *tmp= g_strdup([word UTF8String]);
          int j;
          BOOL lower= NO;
          // if this is a symbol, it's probably in all upper, so we'll try to
          // match whatever case is the user's word
          for (j= 0; tmp[j] && sugg->suggestions[i].name[j]; j++)
          {
            if (islower(tmp[j]))
            {
              sugg->suggestions[i].name[j]= tolower(sugg->suggestions[i].name[j]);
              lower= YES;
            }
          }
          if (lower)
          {
            for (; sugg->suggestions[i].name[j]; j++)
              sugg->suggestions[i].name[j]= tolower(sugg->suggestions[i].name[j]);
          }
        }
        [array addObject:[NSString stringWithUTF8String:sugg->suggestions[i].name]];
      }
    }
    myx_free_syn_suggestions(sugg);
    return array;
  }
  return nil;
}

@end