~ubuntu-branches/ubuntu/raring/monodevelop/raring

« back to all changes in this revision

Viewing changes to src/addins/CBinding/Parser/BsdCTagsManager.cs

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2011-06-29 06:56:25 UTC
  • mfrom: (1.8.1 upstream) (1.3.11 experimental)
  • Revision ID: james.westby@ubuntu.com-20110629065625-7xx19c4vb95j65pl
Tags: 2.5.92+dfsg-1ubuntu1
* Merge from Debian experimental:
 - Dropped patches & changes to debian/control for Moonlight
   + debian/patches/remove_support_for_moonlight.patch,
   + debian/patches/dont_add_moonlight_to_core_addins.patch,
 - Remaining patches:
   + debian/patches/no_appmenu:

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// BsdCTagsManager.cs
 
3
//  
 
4
// Author:
 
5
//       Levi Bard <levi@unity3d.com>
 
6
// 
 
7
// Copyright (c) 2010 Levi Bard
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.IO;
 
28
using System.Text;
 
29
using System.Text.RegularExpressions;
 
30
using System.Collections.Generic;
 
31
 
 
32
using MonoDevelop.Core;
 
33
using MonoDevelop.Core.Execution;
 
34
 
 
35
namespace CBinding.Parser
 
36
{
 
37
        public class BsdCTagsManager: CTagsManager
 
38
        {
 
39
                #region implemented abstract members of CBinding.Parser.CTagsManager
 
40
                
 
41
                protected override IEnumerable<string> GetTags (FileInformation fileInfo)
 
42
                {
 
43
                        string tagFileName = Path.GetFileName (fileInfo.FileName) + ".tag";
 
44
                        string tagFullFileName = Path.Combine (SystemTagsDirectory, tagFileName);
 
45
                        
 
46
                        string ctags_options = string.Format ("-dtx '{0}'", fileInfo.FileName);
 
47
                        string ctags_output = string.Empty;
 
48
                        
 
49
                        if (!File.Exists (tagFullFileName) || File.GetLastWriteTimeUtc (tagFullFileName) < File.GetLastWriteTimeUtc (fileInfo.FileName)) {
 
50
                                ctags_output = GetOutputFromProcess (CTagsExecutable, ctags_options, Environment.CurrentDirectory);
 
51
                                File.WriteAllText (tagFullFileName, ctags_output);
 
52
                        }
 
53
                        
 
54
                        return ctags_output.Split (newlines, StringSplitOptions.RemoveEmptyEntries);
 
55
                }
 
56
                
 
57
                static readonly char[] newlines = {'\r','\n'};
 
58
                protected override IEnumerable<string> GetTags (MonoDevelop.Projects.Project project, string filename, IEnumerable<string> headers)
 
59
                {
 
60
                        StringBuilder ctags_kinds = new StringBuilder ("-dtx");
 
61
                        
 
62
                        ctags_kinds.AppendFormat (" '{0}'", filename);
 
63
                        foreach (string header in headers) {
 
64
                                ctags_kinds.AppendFormat (" '{0}'", header);
 
65
                        }
 
66
                        
 
67
                        string output = GetOutputFromProcess (CTagsExecutable, ctags_kinds.ToString (), project.BaseDirectory);
 
68
                        if (output != null)
 
69
                                return output.Split (newlines, StringSplitOptions.RemoveEmptyEntries);
 
70
                        return null;
 
71
                }
 
72
 
 
73
                public override void FillFileInformation (FileInformation fileInfo)
 
74
                {
 
75
                        IEnumerable<string> ctags_output = GetTags (fileInfo);
 
76
                        if (ctags_output == null) return;
 
77
                        
 
78
                        foreach (string tagEntry in ctags_output) {
 
79
                                if (tagEntry.StartsWith ("!_")) continue;
 
80
                                
 
81
                                Tag tag = ParseTag (tagEntry);
 
82
                                
 
83
                                if (tag != null)
 
84
                                        AddInfo (fileInfo, tag, tagEntry);
 
85
                        }
 
86
                        
 
87
                        fileInfo.IsFilled = true;
 
88
                }
 
89
                
 
90
                
 
91
                // Format: symbol line file fulltext (there may not be any whitespace between symbol and line)
 
92
                static readonly Regex tagExpression = new Regex (@"\s*(?<symbol>[^\s]+?)\s*(?<line>\d+)\s+(?<file>[^\s]+)\s+(?<raw>.*)", RegexOptions.Compiled);
 
93
                
 
94
                public override Tag ParseTag (string tagEntry)
 
95
                {
 
96
                        try {
 
97
                                Match tagMatch = tagExpression.Match (tagEntry);
 
98
                                if (tagMatch == null) return null;
 
99
                                
 
100
                                TagKind kind = TagKind.Member;
 
101
                                string signature = tagMatch.Groups["raw"].Value;
 
102
                                int start = signature.IndexOf ('(');
 
103
                                int end = signature.LastIndexOf (')');
 
104
                                
 
105
                                if (start >= 0 && end > start) {
 
106
                                        // Attempt to parse out method parameter block
 
107
                                        signature = signature.Substring (start, end - start + 1);
 
108
                                        kind = TagKind.Function; // TODO: improve kind guessing
 
109
                                }
 
110
                                return new Tag (tagMatch.Groups["symbol"].Value,
 
111
                                                tagMatch.Groups["file"].Value,
 
112
                                                ulong.Parse (tagMatch.Groups["line"].Value)+1,
 
113
                                                kind, AccessModifier.Public,
 
114
                                                null, null, null, null, null, signature);
 
115
                        } catch (Exception ex) {
 
116
                                LoggingService.LogWarning (string.Format ("Error parsing tag {0}", tagEntry), ex);
 
117
                        }
 
118
                        return null;
 
119
                }
 
120
                
 
121
                #endregion
 
122
                
 
123
        }
 
124
}
 
125