~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/VBNetBinding/Project/Src/LanguageUtils.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Text.RegularExpressions;
 
6
using ICSharpCode.SharpDevelop.Editor;
 
7
 
 
8
namespace ICSharpCode.VBNetBinding
 
9
{
 
10
        public static class LanguageUtils
 
11
        {
 
12
                public static string TrimComments(this string line)
 
13
                {
 
14
                        if (string.IsNullOrEmpty(line))
 
15
                                return string.Empty;
 
16
                        
 
17
                        bool inStr = false;
 
18
                        
 
19
                        for (int i = 0; i < line.Length; i++) {
 
20
                                if (line[i] == '"')
 
21
                                        inStr = !inStr;
 
22
                                if (line[i] == '\'' && !inStr)
 
23
                                        return line.Substring(0, i);
 
24
                        }
 
25
                        
 
26
                        return line;
 
27
                }
 
28
                
 
29
                public static string TrimPreprocessorDirectives(this string line)
 
30
                {
 
31
                        if (string.IsNullOrEmpty(line))
 
32
                                return string.Empty;
 
33
                        
 
34
                        bool wsOnly = true;
 
35
                        
 
36
                        for (int i = 0; i < line.Length; i++) {
 
37
                                if (line[i] == '#' && wsOnly) {
 
38
                                        if (i < line.Length - 1) {
 
39
                                                if (char.IsLetter(line[i + 1]))
 
40
                                                        return line.Substring(0, i);
 
41
                                        } else
 
42
                                                return line.Substring(0, i);
 
43
                                }
 
44
                                if (!char.IsWhiteSpace(line[i]))
 
45
                                        wsOnly = false;
 
46
                        }
 
47
                        
 
48
                        return line;
 
49
                }
 
50
                
 
51
                public static string TrimLine(this string line)
 
52
                {
 
53
                        if (string.IsNullOrEmpty(line))
 
54
                                return string.Empty;
 
55
                        // remove string content
 
56
                        MatchCollection matches = Regex.Matches(line, "\"[^\"]*?\"", RegexOptions.Singleline);
 
57
                        foreach (Match match in matches) {
 
58
                                line = line.Remove(match.Index, match.Length).Insert(match.Index, new string('-', match.Length));
 
59
                        }
 
60
                        // remove comments
 
61
                        return TrimComments(line);
 
62
                }
 
63
                
 
64
                public static bool IsInsideDocumentationComment(ITextEditor editor)
 
65
                {
 
66
                        return IsInsideDocumentationComment(editor, editor.Document.GetLineForOffset(editor.Caret.Offset), editor.Caret.Offset);
 
67
                }
 
68
                
 
69
                public static bool IsInsideDocumentationComment(ITextEditor editor, IDocumentLine curLine, int cursorOffset)
 
70
                {
 
71
                        for (int i = curLine.Offset; i < cursorOffset; ++i) {
 
72
                                char ch = editor.Document.GetCharAt(i);
 
73
                                if (ch == '"')
 
74
                                        return false;
 
75
                                if (ch == '\'' && i + 2 < cursorOffset && editor.Document.GetCharAt(i + 1) == '\'' &&
 
76
                                    editor.Document.GetCharAt(i + 2) == '\'')
 
77
                                        return true;
 
78
                        }
 
79
                        return false;
 
80
                }
 
81
        }
 
82
}