Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5187 from mono/solution-context-filetype-condition
Browse files Browse the repository at this point in the history
Solution context filetype condition
  • Loading branch information
mhutch committed Jun 26, 2018
2 parents 6f1cac5 + ff29658 commit 926494d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
</Extension>

<Extension path = "/MonoDevelop/Ide/ContextMenu/ProjectPad">
<Condition id="ItemType" value="IFileItem">
<Condition id="FileType" fileExtensions=".fs,.fsi,.fsx,.fsscript">
<CommandItem id = "MonoDevelop.FSharp.SolutionPad.DebugScriptInternal"/>
<CommandItem id = "MonoDevelop.FSharp.SolutionPad.DebugScriptExternal"/>
</Condition>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Addins;

Expand All @@ -32,14 +33,13 @@ namespace MonoDevelop.Ide.Extensions
public class FileTypeCondition: ConditionType
{
string fileName;
string mimeType;
List<string> mimeTypeChain;

public void SetFileName (string file)
{
if (file != fileName) {
fileName = file;
if (fileName != null)
mimeType = DesktopService.GetMimeTypeForUri (fileName);
mimeTypeChain = null;
NotifyChanged ();
}
}
Expand All @@ -48,25 +48,42 @@ public override bool Evaluate (NodeElement conditionNode)
{
if (fileName == null)
return false;


string[] fileExtensions = conditionNode.GetAttribute ("fileExtensions").Split (new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
string[] mimeTypes = conditionNode.GetAttribute ("mimeTypes").Split (new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
string[] names = conditionNode.GetAttribute ("name").Split (new char[] {','}, StringSplitOptions.RemoveEmptyEntries);

if (fileExtensions.Length > 0) {
string [] allowedExtensions = conditionNode.GetAttribute ("fileExtensions").Split (new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
if (allowedExtensions.Length > 0) {
string ext = System.IO.Path.GetExtension (fileName);
if (fileExtensions.Any (fe => string.Compare (fe, ext, StringComparison.OrdinalIgnoreCase) == 0))
return true;
foreach (var allowedExtension in allowedExtensions) {
if (string.Equals (ext, allowedExtension, StringComparison.OrdinalIgnoreCase)) {
return true;
}
}
}
if (mimeTypes.Length > 0) {
if (mimeTypes.Any (t => DesktopService.GetMimeTypeIsSubtype (mimeType, t)))
return true;

string[] allowedMimes = conditionNode.GetAttribute ("mimeTypes").Split (new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
if (allowedMimes.Length > 0) {
if (mimeTypeChain == null) {
mimeTypeChain = DesktopService.GetMimeTypeInheritanceChainForFile (fileName).ToList ();
}
foreach (var mimeType in mimeTypeChain) {
foreach (var allowedMime in allowedMimes) {
if (mimeType == allowedMime) {
return true;
}
}
}
}
if (names.Length > 0) {

string[] allowedNames = conditionNode.GetAttribute ("name").Split (new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
if (allowedNames.Length > 0) {
string name = System.IO.Path.GetFileName (fileName);
if (names.Any (fn => string.Compare (fn, name, StringComparison.OrdinalIgnoreCase) == 0))
return true;
foreach (var allowedName in allowedNames) {
if (string.Equals (name, allowedName, StringComparison.OrdinalIgnoreCase)) {
return true;
}
}
}

return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,48 @@
//
// ExtensibleTreeView.cs
//
// Author:
// Lluis Sanchez Gual
// Mike Krüger <mkrueger@novell.com>
//
// Copyright (C) 2005-2008 Novell, Inc (http://www.novell.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.
//

//#define TREE_VERIFY_INTEGRITY

//
// ExtensibleTreeView.cs
//
// Author:
// Lluis Sanchez Gual
// Mike Krüger <mkrueger@novell.com>
//
// Copyright (C) 2005-2008 Novell, Inc (http://www.novell.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.
//

//#define TREE_VERIFY_INTEGRITY

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Mono.Addins;
using MonoDevelop.Core;
using MonoDevelop.Components;
using MonoDevelop.Components.AtkCocoaHelper;
using MonoDevelop.Ide.Commands;
using MonoDevelop.Components.Commands;
using MonoDevelop.Core;
using MonoDevelop.Ide.Commands;
using MonoDevelop.Ide.Extensions;
using MonoDevelop.Ide.Gui.Pads;
using MonoDevelop.Projects.Extensions;
using System.Linq;
using MonoDevelop.Ide.Tasks;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;


namespace MonoDevelop.Ide.Gui.Components
{
public partial class ExtensibleTreeView : Control, ICommandRouter
Expand Down Expand Up @@ -1981,6 +1977,12 @@ CommandEntrySet BuildEntrySet ()
} else {
ExtensionContext ctx = AddinManager.CreateExtensionContext ();
ctx.RegisterCondition ("ItemType", new ItemTypeCondition (tnav.DataItem.GetType (), contextMenuTypeNameAliases));
if (tnav.DataItem is MonoDevelop.Projects.IFileItem fileItem) {
var fileTypeCondition = new FileTypeCondition ();
fileTypeCondition.SetFileName (fileItem.FileName);
ctx.RegisterCondition ("FileType", fileTypeCondition);
}

CommandEntrySet eset = IdeApp.CommandService.CreateCommandEntrySet (ctx, menuPath);

eset.AddItem (Command.Separator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<ConditionType id="ItemType" type="MonoDevelop.Projects.Extensions.ItemTypeCondition">
<Description>Type of the item. If no namespace is provided, MonoDevelop.Projects is assumed.</Description>
</ConditionType>
<ConditionType id="FileType" type="MonoDevelop.Ide.Extensions.FileTypeCondition">
<Description>Type of the file.</Description>
</ConditionType>
</ExtensionPoint>

<Extension path = "/MonoDevelop/Ide/ContextMenu/ProjectPad">
Expand Down

0 comments on commit 926494d

Please sign in to comment.