~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/AspNet/MonoDevelop.AspNet.Mvc/CommandHandlers.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// CommandHandlers.cs
 
3
//  
 
4
// Author:
 
5
//       Piotr Dowgiallo <sparekd@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Piotr Dowgiallo
 
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
 
 
27
using System.Collections.Generic;
 
28
using System.Linq;
 
29
using MonoDevelop.Components.Commands;
 
30
using MonoDevelop.Ide;
 
31
using ICSharpCode.NRefactory.TypeSystem;
 
32
using System.IO;
 
33
using MonoDevelop.Core;
 
34
using MonoDevelop.AspNet.Mvc.Gui;
 
35
using System;
 
36
 
 
37
namespace MonoDevelop.AspNet.Mvc
 
38
{
 
39
        class GoToViewCommandHandler : CommandHandler
 
40
        {
 
41
                protected override void Update (CommandInfo info)
 
42
                {
 
43
                        MvcCommandsCommonHandler.Update (info);
 
44
                }
 
45
 
 
46
                protected override void Run ()
 
47
                {
 
48
                        var doc = IdeApp.Workbench.ActiveDocument;
 
49
                        var currentLocation = doc.Editor.Caret.Location;
 
50
 
 
51
                        var controller = doc.ParsedDocument.GetTopLevelTypeDefinition (currentLocation);
 
52
                        string controllerName = controller.Name;
 
53
                        int pos = controllerName.LastIndexOf ("Controller", StringComparison.Ordinal);
 
54
                        if (pos > 0)
 
55
                                controllerName = controllerName.Remove (pos);
 
56
 
 
57
                        var baseDirectory = doc.FileName.ParentDirectory.ParentDirectory;
 
58
 
 
59
                        string actionName = doc.ParsedDocument.GetMember (currentLocation).Name;
 
60
                        var viewFoldersPaths = new FilePath[] {
 
61
                                baseDirectory.Combine ("Views", controllerName),
 
62
                                baseDirectory.Combine ("Views", "Shared")
 
63
                        };
 
64
                        var viewExtensions = new string[] { ".aspx", ".cshtml" };
 
65
 
 
66
                        foreach (var folder in viewFoldersPaths) {
 
67
                                foreach (var ext in viewExtensions) {
 
68
                                        var possibleFile = folder.Combine (actionName + ext);
 
69
                                        if (File.Exists (possibleFile)) {
 
70
                                                IdeApp.Workbench.OpenDocument (possibleFile);
 
71
                                                return;
 
72
                                        }
 
73
                                }
 
74
                        }
 
75
 
 
76
                        MessageService.ShowError ("Matching view cannot be found.");
 
77
                }
 
78
        }
 
79
 
 
80
        class AddViewFromControllerCommandHandler : CommandHandler
 
81
        {
 
82
                protected override void Update (CommandInfo info)
 
83
                {
 
84
                        MvcCommandsCommonHandler.Update (info);
 
85
                }
 
86
 
 
87
                protected override void Run ()
 
88
                {
 
89
                        var doc = IdeApp.Workbench.ActiveDocument;
 
90
                        var project = doc.Project as AspMvcProject;
 
91
                        var currentLocation = doc.Editor.Caret.Location;
 
92
 
 
93
                        string controllerName = doc.ParsedDocument.GetTopLevelTypeDefinition (currentLocation).Name;
 
94
                        int pos = controllerName.LastIndexOf ("Controller", StringComparison.Ordinal);
 
95
                        if (pos > 0)
 
96
                                controllerName = controllerName.Remove (pos);
 
97
 
 
98
                        string path = doc.FileName.ParentDirectory.ParentDirectory.Combine ("Views", controllerName);
 
99
                        string actionName = doc.ParsedDocument.GetMember (currentLocation).Name;
 
100
                        FolderCommandHandler.AddView (project, path, actionName);
 
101
                }
 
102
        }
 
103
 
 
104
        class GoToControllerCommandHandler : CommandHandler
 
105
        {
 
106
                protected override void Update (CommandInfo info)
 
107
                {
 
108
                        var doc = IdeApp.Workbench.ActiveDocument;
 
109
                        if (doc == null || !(doc.Project is AspMvcProject)) {
 
110
                                info.Enabled = info.Visible = false;
 
111
                                return;
 
112
                        }
 
113
                        var baseDirectory = doc.FileName.ParentDirectory.ParentDirectory;
 
114
                        if (!string.Equals (baseDirectory.FileName, "Views"))
 
115
                                info.Enabled = info.Visible = false;
 
116
                }
 
117
 
 
118
                protected override void Run ()
 
119
                {
 
120
                        var doc = IdeApp.Workbench.ActiveDocument;
 
121
                        var name = doc.FileName.ParentDirectory.FileName;
 
122
                        var controller = doc.ProjectContent.GetAllTypeDefinitions ().FirstOrDefault (t => t.Name == name + "Controller");
 
123
 
 
124
                        if (controller != null)
 
125
                                IdeApp.Workbench.OpenDocument (controller.UnresolvedFile.FileName);
 
126
                        else
 
127
                                MessageService.ShowError ("Matching controller cannot be found.");
 
128
                }
 
129
        }
 
130
 
 
131
        static class MvcCommandsCommonHandler
 
132
        {
 
133
                public static void Update (CommandInfo info)
 
134
                {
 
135
                        var doc = IdeApp.Workbench.ActiveDocument;
 
136
                        if (doc == null || !(doc.Project is AspMvcProject)) {
 
137
                                info.Enabled = info.Visible = false;
 
138
                                return;
 
139
                        }
 
140
 
 
141
                        var currentLocation = doc.Editor.Caret.Location;
 
142
                        var topLevelType = doc.ParsedDocument.GetTopLevelTypeDefinition (currentLocation);
 
143
                        if (topLevelType == null || !topLevelType.Name.EndsWith ("Controller", StringComparison.Ordinal)) {
 
144
                                info.Enabled = info.Visible = false;
 
145
                                return;
 
146
                        }
 
147
 
 
148
                        var correctReturnTypes = new string[] { "ActionResult", "ViewResultBase", "ViewResult", "PartialViewResult" };
 
149
                        var member = doc.ParsedDocument.GetMember (currentLocation) as IUnresolvedMethod;
 
150
                        if (member == null || !member.IsPublic || !correctReturnTypes.Any (t => t == member.ReturnType.ToString ()))
 
151
                                info.Enabled = info.Visible = false;
 
152
                }
 
153
        }
 
154
}
 
155