~ubuntu-branches/ubuntu/natty/monodevelop/natty

« back to all changes in this revision

Viewing changes to src/addins/CSharpBinding/Syntax/HighlightCSharpSemanticRule.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-01-07 19:06:58 UTC
  • mto: (1.6.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 46.
  • Revision ID: james.westby@ubuntu.com-20100107190658-z9z95lgk4kwfes7p
ImportĀ upstreamĀ versionĀ 2.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// HighlightPropertiesSemanticRule.cs
3
 
//
4
 
// Author:
5
 
//   Mike KrĆ¼ger <mkrueger@novell.com>
6
 
//
7
 
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8
 
//
9
 
// Permission is hereby granted, free of charge, to any person obtaining
10
 
// a copy of this software and associated documentation files (the
11
 
// "Software"), to deal in the Software without restriction, including
12
 
// without limitation the rights to use, copy, modify, merge, publish,
13
 
// distribute, sublicense, and/or sell copies of the Software, and to
14
 
// permit persons to whom the Software is furnished to do so, subject to
15
 
// the following conditions:
16
 
// 
17
 
// The above copyright notice and this permission notice shall be
18
 
// included in all copies or substantial portions of the Software.
19
 
// 
20
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
 
//
28
 
 
29
 
using System;
30
 
using System.Collections.Generic;
31
 
 
32
 
using Mono.TextEditor;
33
 
using Mono.TextEditor.Highlighting;
34
 
using MonoDevelop.Ide.Gui;
35
 
using MonoDevelop.Projects;
36
 
using MonoDevelop.Projects.Dom;
37
 
using MonoDevelop.Projects.Dom.Parser;
38
 
using MonoDevelop.Projects.Gui.Completion;
39
 
using System.Text;
40
 
using ICSharpCode.NRefactory.Ast;
41
 
 
42
 
namespace MonoDevelop.CSharpBinding
43
 
{
44
 
        class HighlightCSharpSemanticRule : SemanticRule
45
 
        {
46
 
                ProjectDom ctx;
47
 
//              Mono.TextEditor.Document document;
48
 
//              MonoDevelop.Ide.Gui.Document doc;
49
 
//              IParser parser;
50
 
//              IResolver resolver;
51
 
//              IExpressionFinder expressionFinder;
52
 
                
53
 
/*              void Init (Mono.TextEditor.Document document)
54
 
                {
55
 
                        
56
 
//                      parser = ProjectDomService.GetParser (document.FileName, document.MimeType);
57
 
//                      expressionFinder = ProjectDomService.GetExpressionFinder (document.FileName);
58
 
                }*/
59
 
                
60
 
                
61
 
                ProjectDom GetParserContext (Mono.TextEditor.Document document)
62
 
                {
63
 
                        Project project = IdeApp.ProjectOperations.CurrentSelectedProject;
64
 
                        if (project != null)
65
 
                                return ProjectDomService.GetProjectDom (project);
66
 
                        return ProjectDom.Empty;
67
 
                }
68
 
                
69
 
        //      string expression;
70
 
/*              IMember GetLanguageItem (Mono.TextEditor.Document document, LineSegment line, int offset, string expression)
71
 
                {
72
 
                        string txt = document.Text;
73
 
                        ExpressionResult expressionResult = new ExpressionResult (expression);
74
 
//                      ExpressionResult expressionResult = expressionFinder.FindFullExpression (txt, offset);
75
 
                        int lineNumber = document.OffsetToLineNumber (offset);
76
 
                        expressionResult.Region = new DomRegion (lineNumber, offset - line.Offset, lineNumber, offset + expression.Length - line.Offset);
77
 
                        expressionResult.ExpressionContext = ExpressionContext.IdentifierExpected;
78
 
                        
79
 
                        resolver = new NRefactoryResolver (ctx, doc.CompilationUnit, doc.TextEditor, document.FileName);
80
 
                        ResolveResult result = resolver.Resolve (expressionResult, expressionResult.Region.Start);
81
 
                        
82
 
                        if (result is MemberResolveResult) 
83
 
                                return ((MemberResolveResult)result).ResolvedMember;
84
 
                        return null;
85
 
                }*/
86
 
                
87
 
                public override void Analyze (Mono.TextEditor.Document doc, LineSegment line, Chunk startChunk, int startOffset, int endOffset)
88
 
                {
89
 
                        if (!MonoDevelop.Core.PropertyService.Get ("EnableSemanticHighlighting", false) || doc == null || line == null || startChunk == null)
90
 
                                return;
91
 
                        ctx = GetParserContext (doc);
92
 
                        int lineNumber = doc.OffsetToLineNumber (line.Offset);
93
 
                        ParsedDocument parsedDocument = ProjectDomService.GetParsedDocument (ctx, doc.FileName);
94
 
                        if (parsedDocument == null || parsedDocument.CompilationUnit == null)
95
 
                                return;
96
 
                        for (Chunk chunk = startChunk; chunk != null; chunk = chunk.Next) {
97
 
                                if (chunk.Style != "text")
98
 
                                        continue;
99
 
                                for (int i = chunk.Offset; i < chunk.EndOffset; i++) {
100
 
                                        char charBefore = i > 0 ? doc.GetCharAt (i - 1) : '}';
101
 
                                        if (Char.IsLetter (doc.GetCharAt (i)) && !Char.IsLetterOrDigit (charBefore)) {
102
 
                                        } else {
103
 
                                                continue;
104
 
                                        }
105
 
                                        
106
 
                                        int start = i;
107
 
                                        bool wasWhitespace = false;
108
 
                                        bool wasDot = false;
109
 
                                        int bracketCount = 0;
110
 
                                        while (start > 0) {
111
 
                                                char ch = doc.GetCharAt (start);
112
 
                                                if (wasWhitespace && IsNamePart(ch)) {
113
 
                                                        start++;
114
 
                                                        if (start < chunk.Offset)
115
 
                                                                start = Int32.MaxValue;
116
 
                                                        break;
117
 
                                                }
118
 
                                                if (ch == '<') {
119
 
                                                        bracketCount--;
120
 
                                                        if (bracketCount < 0) {
121
 
                                                                start++;
122
 
                                                                break; 
123
 
                                                        }
124
 
                                                        start--;
125
 
                                                        wasWhitespace = false;
126
 
                                                        continue;
127
 
                                                }
128
 
                                                if (ch == '>') {
129
 
                                                        if (wasWhitespace && !wasDot)
130
 
                                                                break;
131
 
                                                        bracketCount++;
132
 
                                                        start--;
133
 
                                                        wasWhitespace = false;
134
 
                                                        continue;
135
 
                                                }
136
 
                                                if (!IsNamePart(ch) && !Char.IsWhiteSpace (ch) && ch != '.') {
137
 
                                                        start++;
138
 
                                                        break;
139
 
                                                }
140
 
                                                wasWhitespace = Char.IsWhiteSpace (ch);
141
 
                                                wasDot = ch == '.' || wasDot && wasWhitespace;
142
 
                                                start--;
143
 
                                        }
144
 
                                        
145
 
                                        int end = i;
146
 
                                        int genericCount = 0;
147
 
                                        wasWhitespace = false;
148
 
                                        List<Segment> nameSegments = new List<Segment> ();
149
 
                                        while (end < chunk.EndOffset) {
150
 
                                                char ch = doc.GetCharAt (end);
151
 
                                                if (wasWhitespace && IsNamePart(ch))
152
 
                                                        break;
153
 
                                                if (ch == '<') {
154
 
                                                        genericCount = 1;
155
 
                                                        while (end < doc.Length) {
156
 
                                                                ch = doc.GetCharAt (end);
157
 
                                                                if (ch == ',')
158
 
                                                                        genericCount++;
159
 
                                                                if (ch == '>') {
160
 
                                                                        nameSegments.Add (new Segment (end, 1));
161
 
                                                                        break;
162
 
                                                                }
163
 
                                                                end++;
164
 
                                                        }
165
 
                                                        break;
166
 
                                                }
167
 
                                                if (!IsNamePart(ch) && !Char.IsWhiteSpace (ch)) 
168
 
                                                        break;
169
 
                                                wasWhitespace = Char.IsWhiteSpace (ch);
170
 
                                                end++;
171
 
                                        }
172
 
                                        if (start >= end) 
173
 
                                                continue;
174
 
                                        string typeString = doc.GetTextBetween (start, end);
175
 
                                        IReturnType returnType = NRefactoryResolver.ParseReturnType (new ExpressionResult (typeString));
176
 
                                        
177
 
                                        int nameEndOffset = start;
178
 
                                        for (; nameEndOffset < end; nameEndOffset++) {
179
 
                                                char ch = doc.GetCharAt (nameEndOffset);
180
 
                                                if (nameEndOffset >= i && ch == '<') {
181
 
                                                        nameEndOffset++;
182
 
                                                        break;
183
 
                                                }
184
 
                                        }
185
 
                                        nameSegments.Add (new Segment (i, nameEndOffset - i));
186
 
 
187
 
                                        int column = i - line.Offset;
188
 
                                        IType callingType = parsedDocument.CompilationUnit.GetTypeAt (lineNumber, column);
189
 
                                        List<IReturnType> genericParams = null;
190
 
                                        if (genericCount > 0) {
191
 
                                                genericParams = new List<IReturnType> ();
192
 
                                                for (int n = 0; n < genericCount; n++) 
193
 
                                                        genericParams.Add (new DomReturnType ("A"));
194
 
                                        }
195
 
                                        
196
 
                                        IType type = ctx.SearchType (new SearchTypeRequest (parsedDocument.CompilationUnit, returnType, callingType));
197
 
                                        if (type != null)
198
 
                                                nameSegments.ForEach (segment => HighlightSegment (startChunk, segment, "keyword.semantic.type"));
199
 
                                }
200
 
                        }
201
 
                }
202
 
 
203
 
                static void HighlightSegment (Chunk startChunk, Segment namePart, string style)
204
 
                {
205
 
                        for (Chunk searchChunk = startChunk; searchChunk != null; searchChunk = searchChunk.Next) {
206
 
                                if (!searchChunk.Contains (namePart))
207
 
                                        continue;
208
 
                                if (searchChunk.Length == namePart.Length) {
209
 
                                        searchChunk.Style = style;
210
 
                                        break;
211
 
                                }
212
 
                                Chunk propertyChunk = new Chunk (namePart.Offset, namePart.Length, style);
213
 
                                propertyChunk.Next = searchChunk.Next;
214
 
                                searchChunk.Next = propertyChunk;
215
 
                                if (searchChunk.EndOffset - propertyChunk.EndOffset > 0) {
216
 
                                        Chunk newChunk = new Chunk (propertyChunk.EndOffset, searchChunk.EndOffset - propertyChunk.EndOffset, searchChunk.Style);
217
 
                                        newChunk.Next = propertyChunk.Next;
218
 
                                        propertyChunk.Next = newChunk;
219
 
                                }
220
 
                                searchChunk.Length = namePart.Offset - searchChunk.Offset;
221
 
                                break;
222
 
                        }
223
 
                }
224
 
 
225
 
 
226
 
                bool IsNamePart (char ch)
227
 
                {
228
 
                        return Char.IsLetterOrDigit (ch) || ch == '_';
229
 
                }
230
 
        }
231
 
}