|
Line 0
Link Here
|
|
|
1 |
// |
| 2 |
// SwapCodeBehindCommand.cs |
| 3 |
// |
| 4 |
// Author: |
| 5 |
// cwensley <> |
| 6 |
// |
| 7 |
// Copyright (c) 2009 cwensley |
| 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; |
| 28 |
using MonoDevelop.Components.Commands; |
| 29 |
using System.IO; |
| 30 |
using MonoDevelop.Projects; |
| 31 |
using MonoDevelop.Ide.Gui; |
| 32 |
using System.Linq; |
| 33 |
|
| 34 |
namespace MonoDevelop.AspNet |
| 35 |
{ |
| 36 |
/// <summary> |
| 37 |
/// Swaps between the .aspx/.ascx/etc and the code behind file |
| 38 |
/// </summary> |
| 39 |
public class SwapCodeBehindCommand : CommandHandler |
| 40 |
{ |
| 41 |
// TODO: Move to AspNetAppProject? |
| 42 |
public static string[] HeaderExtensions = { |
| 43 |
".ASPX", |
| 44 |
".ASCX", |
| 45 |
".ASHX", |
| 46 |
".MASTER" |
| 47 |
}; |
| 48 |
|
| 49 |
// TODO: Move to AspNetAppProject? |
| 50 |
public static bool IsHeaderFile (string filename) |
| 51 |
{ |
| 52 |
string extension = Path.GetExtension (filename).ToUpper (); |
| 53 |
return HeaderExtensions.Any(a => a.EndsWith(extension)); |
| 54 |
} |
| 55 |
|
| 56 |
// TODO: Move to AspNetAppProject? |
| 57 |
public string MatchingFile (AspNetAppProject ap, string sourceFile) { |
| 58 |
string filenameStub; |
| 59 |
if (IsHeaderFile(sourceFile)) |
| 60 |
filenameStub = ap.LanguageBinding.GetFileName(sourceFile); |
| 61 |
else if (ap.LanguageBinding.IsSourceCodeFile(sourceFile)) |
| 62 |
filenameStub = Path.Combine(Path.GetDirectoryName(sourceFile), Path.GetFileNameWithoutExtension (sourceFile)); |
| 63 |
else |
| 64 |
return null; |
| 65 |
|
| 66 |
ProjectFile file = ap.GetProjectFile(filenameStub); |
| 67 |
if (file != null) |
| 68 |
return file.Name; |
| 69 |
|
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
protected override void Run () |
| 74 |
{ |
| 75 |
var doc = IdeApp.Workbench.ActiveDocument; |
| 76 |
if (doc == null) |
| 77 |
return; |
| 78 |
|
| 79 |
var ap = doc.Project as AspNetAppProject; |
| 80 |
if (ap != null) { |
| 81 |
string match = MatchingFile (ap, doc.FileName); |
| 82 |
|
| 83 |
if (match != null) |
| 84 |
IdeApp.Workbench.OpenDocument (match); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
protected override void Update (CommandInfo info) |
| 89 |
{ |
| 90 |
info.Visible = false; |
| 91 |
|
| 92 |
var doc = IdeApp.Workbench.ActiveDocument; |
| 93 |
if (doc == null) |
| 94 |
return; |
| 95 |
|
| 96 |
var ap = doc.Project as AspNetAppProject; |
| 97 |
if (ap == null) |
| 98 |
return; |
| 99 |
|
| 100 |
info.Visible = true; |
| 101 |
|
| 102 |
string filename = doc.FileName; |
| 103 |
info.Enabled = (IsHeaderFile (filename) || ap.LanguageBinding.IsSourceCodeFile(filename)) |
| 104 |
&& MatchingFile (ap, doc.FileName) != null; |
| 105 |
} |
| 106 |
} |
| 107 |
} |