5
// Piotr Dowgiallo <sparekd@gmail.com>
7
// Copyright (c) 2012 Piotr Dowgiallo
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
27
using System.Collections.Generic;
29
using MonoDevelop.Components.Commands;
30
using MonoDevelop.Ide;
31
using ICSharpCode.NRefactory.TypeSystem;
33
using MonoDevelop.Core;
34
using MonoDevelop.AspNet.Mvc.Gui;
37
namespace MonoDevelop.AspNet.Mvc
39
class GoToViewCommandHandler : CommandHandler
41
protected override void Update (CommandInfo info)
43
MvcCommandsCommonHandler.Update (info);
46
protected override void Run ()
48
var doc = IdeApp.Workbench.ActiveDocument;
49
var currentLocation = doc.Editor.Caret.Location;
51
var controller = doc.ParsedDocument.GetTopLevelTypeDefinition (currentLocation);
52
string controllerName = controller.Name;
53
int pos = controllerName.LastIndexOf ("Controller", StringComparison.Ordinal);
55
controllerName = controllerName.Remove (pos);
57
var baseDirectory = doc.FileName.ParentDirectory.ParentDirectory;
59
string actionName = doc.ParsedDocument.GetMember (currentLocation).Name;
60
var viewFoldersPaths = new FilePath[] {
61
baseDirectory.Combine ("Views", controllerName),
62
baseDirectory.Combine ("Views", "Shared")
64
var viewExtensions = new string[] { ".aspx", ".cshtml" };
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);
76
MessageService.ShowError ("Matching view cannot be found.");
80
class AddViewFromControllerCommandHandler : CommandHandler
82
protected override void Update (CommandInfo info)
84
MvcCommandsCommonHandler.Update (info);
87
protected override void Run ()
89
var doc = IdeApp.Workbench.ActiveDocument;
90
var project = doc.Project as AspMvcProject;
91
var currentLocation = doc.Editor.Caret.Location;
93
string controllerName = doc.ParsedDocument.GetTopLevelTypeDefinition (currentLocation).Name;
94
int pos = controllerName.LastIndexOf ("Controller", StringComparison.Ordinal);
96
controllerName = controllerName.Remove (pos);
98
string path = doc.FileName.ParentDirectory.ParentDirectory.Combine ("Views", controllerName);
99
string actionName = doc.ParsedDocument.GetMember (currentLocation).Name;
100
FolderCommandHandler.AddView (project, path, actionName);
104
class GoToControllerCommandHandler : CommandHandler
106
protected override void Update (CommandInfo info)
108
var doc = IdeApp.Workbench.ActiveDocument;
109
if (doc == null || !(doc.Project is AspMvcProject)) {
110
info.Enabled = info.Visible = false;
113
var baseDirectory = doc.FileName.ParentDirectory.ParentDirectory;
114
if (!string.Equals (baseDirectory.FileName, "Views"))
115
info.Enabled = info.Visible = false;
118
protected override void Run ()
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");
124
if (controller != null)
125
IdeApp.Workbench.OpenDocument (controller.UnresolvedFile.FileName);
127
MessageService.ShowError ("Matching controller cannot be found.");
131
static class MvcCommandsCommonHandler
133
public static void Update (CommandInfo info)
135
var doc = IdeApp.Workbench.ActiveDocument;
136
if (doc == null || !(doc.Project is AspMvcProject)) {
137
info.Enabled = info.Visible = false;
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;
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;