diff options
Diffstat (limited to 'main/src/addins/AspNet/Commands')
6 files changed, 155 insertions, 46 deletions
diff --git a/main/src/addins/AspNet/Commands/AddViewDialog.cs b/main/src/addins/AspNet/Commands/AddViewDialog.cs index 752fc51522..8525483503 100644 --- a/main/src/addins/AspNet/Commands/AddViewDialog.cs +++ b/main/src/addins/AspNet/Commands/AddViewDialog.cs @@ -29,15 +29,17 @@ using System.IO; using System.Collections.Generic; using PP = System.IO.Path; -using MonoDevelop.Ide; +using Gtk; +using ICSharpCode.NRefactory6.CSharp; +using Microsoft.CodeAnalysis; using MonoDevelop.Core; -using ICSharpCode.NRefactory.TypeSystem; +using MonoDevelop.Ide; using MonoDevelop.Ide.TypeSystem; using MonoDevelop.AspNet.Projects; -using MonoDevelop.AspNet.WebForms.Dom; using MonoDevelop.AspNet.WebForms; using Gtk; using MonoDevelop.Projects; +using MonoDevelop.AspNet.WebForms.Dom; namespace MonoDevelop.AspNet.Commands { @@ -366,7 +368,7 @@ namespace MonoDevelop.AspNet.Commands if (!File.Exists (realPath)) return; - var pd = TypeSystemService.ParseFile (project, realPath) as WebFormsParsedDocument; + var pd = TypeSystemService.ParseFile (project, realPath).Result as WebFormsParsedDocument; if (pd != null) { try { @@ -470,22 +472,18 @@ namespace MonoDevelop.AspNet.Commands class TypeDataProvider
{
- public List<ITypeDefinition> TypesList { get; private set; }
+ public List<INamedTypeSymbol> TypesList { get; private set; }
public List<string> TypeNamesList { get; private set; }
- Ambience ambience;
public TypeDataProvider (MonoDevelop.Projects.DotNetProject project)
{
- TypeNamesList = new List<string> ();
- var ctx = TypeSystemService.GetCompilation (project);
- TypesList = new List<ITypeDefinition> (ctx.MainAssembly.GetAllTypeDefinitions ());
- this.ambience = AmbienceService.GetAmbience (project.LanguageName);
+ TypeNamesList = new List<string> (); + var ctx = TypeSystemService.GetCompilationAsync (project).Result; + TypesList = new List<INamedTypeSymbol> (ctx.GetAllTypesInMainAssembly ());
foreach (var typeDef in TypesList) {
- TypeNamesList.Add (ambience.GetString ((IEntity)typeDef, OutputFlags.IncludeGenerics | OutputFlags.UseFullName | OutputFlags.IncludeMarkup));
+ TypeNamesList.Add (Ambience.EscapeText (typeDef.ToDisplayString (SymbolDisplayFormat.CSharpErrorMessageFormat))); }
}
} } } - - diff --git a/main/src/addins/AspNet/Commands/AddViewFromControllerCommandHandler.cs b/main/src/addins/AspNet/Commands/AddViewFromControllerCommandHandler.cs index 0d732c6f54..ed0bd44867 100644 --- a/main/src/addins/AspNet/Commands/AddViewFromControllerCommandHandler.cs +++ b/main/src/addins/AspNet/Commands/AddViewFromControllerCommandHandler.cs @@ -43,16 +43,12 @@ namespace MonoDevelop.AspNet.Commands {
var doc = IdeApp.Workbench.ActiveDocument;
var project = (DotNetProject)doc.Project;
- var currentLocation = doc.Editor.Caret.Location;
- string controllerName = doc.ParsedDocument.GetTopLevelTypeDefinition (currentLocation).Name;
- int pos = controllerName.LastIndexOf ("Controller", StringComparison.Ordinal);
- if (pos > 0)
- controllerName = controllerName.Remove (pos);
+ var method = MethodDeclarationAtCaret.Create (doc);
+ string controllerName = method.GetParentMvcControllerName ();
+ string path = doc.FileName.ParentDirectory.ParentDirectory.Combine ("Views", controllerName);
- string path = doc.FileName.ParentDirectory.ParentDirectory.Combine ("Views", controllerName); - string actionName = doc.ParsedDocument.GetMember (currentLocation).Name;
- AddView (project, path, actionName);
+ AddView (project, path, method.Name);
}
public static void AddView (DotNetProject project, string path, string name)
diff --git a/main/src/addins/AspNet/Commands/GoToControllerCommandHandler.cs b/main/src/addins/AspNet/Commands/GoToControllerCommandHandler.cs index 67c80ed920..fb944f910e 100644 --- a/main/src/addins/AspNet/Commands/GoToControllerCommandHandler.cs +++ b/main/src/addins/AspNet/Commands/GoToControllerCommandHandler.cs @@ -26,15 +26,16 @@ using System;
using System.Linq;
-using ICSharpCode.NRefactory.TypeSystem;
+using ICSharpCode.NRefactory6.CSharp;
+using Microsoft.CodeAnalysis;
+using MonoDevelop.AspNet.Projects;
using MonoDevelop.Components.Commands;
using MonoDevelop.Core;
using MonoDevelop.Ide;
-using MonoDevelop.AspNet.Projects;
+using MonoDevelop.Ide.TypeSystem;
namespace MonoDevelop.AspNet.Commands
{
-
class GoToControllerCommandHandler : CommandHandler
{
protected override void Update (CommandInfo info)
@@ -58,13 +59,22 @@ namespace MonoDevelop.AspNet.Commands {
var doc = IdeApp.Workbench.ActiveDocument;
var name = doc.FileName.ParentDirectory.FileName;
- var controller = doc.ProjectContent.GetAllTypeDefinitions ().FirstOrDefault (t => t.Name == name + "Controller");
+ var controller = FindController (doc.Project, name);
if (controller != null)
- IdeApp.Workbench.OpenDocument (controller.UnresolvedFile.FileName, doc.Project);
+ IdeApp.ProjectOperations.JumpToDeclaration (controller, doc.Project);
else
MessageService.ShowError ("Matching controller cannot be found.");
}
+
+ INamedTypeSymbol FindController (MonoDevelop.Projects.Project project, string name)
+ {
+ var compilation = TypeSystemService.GetCompilationAsync (project).Result;
+ if (compilation == null)
+ return null;
+
+ return compilation.GetAllTypesInMainAssembly ()
+ .FirstOrDefault (symbol => symbol.Name == name + "Controller");
+ }
}
-
}
diff --git a/main/src/addins/AspNet/Commands/GoToViewCommandHandler.cs b/main/src/addins/AspNet/Commands/GoToViewCommandHandler.cs index cbdfed0257..f84173bdca 100644 --- a/main/src/addins/AspNet/Commands/GoToViewCommandHandler.cs +++ b/main/src/addins/AspNet/Commands/GoToViewCommandHandler.cs @@ -26,7 +26,6 @@ using System;
using System.IO;
-using ICSharpCode.NRefactory.TypeSystem;
using MonoDevelop.Components.Commands;
using MonoDevelop.Core;
using MonoDevelop.Ide;
@@ -43,17 +42,12 @@ namespace MonoDevelop.AspNet.Commands protected override void Run ()
{
var doc = IdeApp.Workbench.ActiveDocument;
- var currentLocation = doc.Editor.Caret.Location;
+ var method = MethodDeclarationAtCaret.Create (doc);
+ string controllerName = method.GetParentMvcControllerName ();
- var controller = doc.ParsedDocument.GetTopLevelTypeDefinition (currentLocation); - string controllerName = controller.Name; - int pos = controllerName.LastIndexOf ("Controller", StringComparison.Ordinal);
- if (pos > 0)
- controllerName = controllerName.Remove (pos);
- var baseDirectory = doc.FileName.ParentDirectory.ParentDirectory; - string actionName = doc.ParsedDocument.GetMember (currentLocation).Name;
+ string actionName = method.Name;
var viewFoldersPaths = new [] {
baseDirectory.Combine ("Views", controllerName), baseDirectory.Combine ("Views", "Shared") diff --git a/main/src/addins/AspNet/Commands/MethodDeclarationAtCaret.cs b/main/src/addins/AspNet/Commands/MethodDeclarationAtCaret.cs new file mode 100644 index 0000000000..83ba6b4db2 --- /dev/null +++ b/main/src/addins/AspNet/Commands/MethodDeclarationAtCaret.cs @@ -0,0 +1,118 @@ +// +// MethodAtCaret.cs +// +// Author: +// Matt Ward <matt.ward@xamarin.com> +// +// Copyright (c) 2015 Xamarin Inc. (http://xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; +using System.Linq; +using MonoDevelop.Ide.Gui; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Text; +using Microsoft.CodeAnalysis.CSharp; + +namespace MonoDevelop.AspNet.Commands +{ + class MethodDeclarationAtCaret + { + MethodDeclarationAtCaret () + { + } + + public static readonly MethodDeclarationAtCaret NullMethodDeclaration = new MethodDeclarationAtCaret (); + + public bool IsMethodFound { + get { + return TypeDeclaration != null && MethodDeclaration != null; + } + } + + public TypeDeclarationSyntax TypeDeclaration { get; private set; } + public MethodDeclarationSyntax MethodDeclaration { get; private set; } + + public string Name { + get { + if (MethodDeclaration != null) + return MethodDeclaration.Identifier.ValueText; + return String.Empty; + } + } + + public static MethodDeclarationAtCaret Create (MonoDevelop.Ide.Gui.Document doc) + { + var parsedDocument = doc.AnalysisDocument; + if (parsedDocument == null) + return NullMethodDeclaration; + + SyntaxNode root = null; + if (!parsedDocument.TryGetSyntaxRoot (out root)) + return NullMethodDeclaration; + + SyntaxNode currentNode; + try { + int caretOffset = doc.Editor.CaretOffset; + currentNode = root.FindNode (TextSpan.FromBounds (caretOffset, caretOffset)); + } catch (Exception) { + return NullMethodDeclaration; + } + + var currentType = currentNode.AncestorsAndSelf ().OfType<TypeDeclarationSyntax> ().FirstOrDefault (); + var currentMethod = currentNode.AncestorsAndSelf ().OfType<MethodDeclarationSyntax> ().FirstOrDefault (); + + return new MethodDeclarationAtCaret { + TypeDeclaration = currentType, + MethodDeclaration = currentMethod + }; + } + + public bool IsParentMvcController () + { + return TypeDeclaration.Identifier.ValueText.EndsWith ("Controller", StringComparison.OrdinalIgnoreCase); + } + + public bool IsMvcViewMethod () + { + var correctReturnTypes = new [] { "ActionResult", "ViewResultBase", "ViewResult", "PartialViewResult" }; + + string returnTypeName = MethodDeclaration.ReturnType.ToString (); + + return MethodDeclaration.Modifiers.Any (t => t.Kind () == SyntaxKind.PublicKeyword) && + correctReturnTypes.Any (t => t == returnTypeName); + } + + public string GetParentMvcControllerName () + { + if (TypeDeclaration == null) + return String.Empty; + + string controllerName = TypeDeclaration.Identifier.ValueText; + int pos = controllerName.LastIndexOf ("Controller", StringComparison.Ordinal); + if (pos > 0) + controllerName = controllerName.Remove (pos); + + return controllerName; + } + } +} + diff --git a/main/src/addins/AspNet/Commands/MvcCommandsCommonHandler.cs b/main/src/addins/AspNet/Commands/MvcCommandsCommonHandler.cs index efa6c6493c..8f8acfc539 100644 --- a/main/src/addins/AspNet/Commands/MvcCommandsCommonHandler.cs +++ b/main/src/addins/AspNet/Commands/MvcCommandsCommonHandler.cs @@ -26,7 +26,6 @@ using System;
using System.Linq;
-using ICSharpCode.NRefactory.TypeSystem;
using MonoDevelop.Components.Commands;
using MonoDevelop.Ide;
using MonoDevelop.AspNet.Projects;
@@ -48,17 +47,11 @@ namespace MonoDevelop.AspNet.Commands return;
}
- var currentLocation = doc.Editor.Caret.Location;
- var topLevelType = doc.ParsedDocument.GetTopLevelTypeDefinition (currentLocation);
- if (topLevelType == null || !topLevelType.Name.EndsWith ("Controller", StringComparison.Ordinal)) { - info.Enabled = info.Visible = false;
+ var method = MethodDeclarationAtCaret.Create (doc);
+ if (method.IsMethodFound && method.IsParentMvcController () && method.IsMvcViewMethod ())
return;
- }
- var correctReturnTypes = new [] { "ActionResult", "ViewResultBase", "ViewResult", "PartialViewResult" };
- var member = doc.ParsedDocument.GetMember (currentLocation) as IUnresolvedMethod;
- if (member == null || !member.IsPublic || correctReturnTypes.All (t => t != member.ReturnType.ToString ()))
- info.Enabled = info.Visible = false;
+ info.Enabled = info.Visible = false;
}
}
}
|