diff options
author | Tak <levi@unity3d.com> | 2012-01-04 19:39:52 +0400 |
---|---|---|
committer | Tak <levi@unity3d.com> | 2012-01-04 19:39:52 +0400 |
commit | 8ef839f8224d606dc7073aa14aeef2a96f1e395e (patch) | |
tree | 00ff02ea726aff0a35534d11247aa740a4c897d7 /extras/PyBinding | |
parent | ef76ac65e275380b6cd58e20f66ab39eb5bedbad (diff) |
[PyBinding] Add pathbar support to PyBinding.
License: MIT/X11
Diffstat (limited to 'extras/PyBinding')
5 files changed, 332 insertions, 1 deletions
diff --git a/extras/PyBinding/PyBinding/Makefile b/extras/PyBinding/PyBinding/Makefile index f2f98f2fc8..c01d00f6ed 100644 --- a/extras/PyBinding/PyBinding/Makefile +++ b/extras/PyBinding/PyBinding/Makefile @@ -63,6 +63,8 @@ FILES = \ PyBinding.Gui.Navigation/PackageNodeBuilder.cs \ PyBinding.Gui.Navigation/PackagesNodeBuilder.cs \ PyBinding.Gui.Navigation/ProjectNodeBuilderExtension.cs \ + PyBinding.Gui/CompilationUnitDataProvider.cs \ + PyBinding.Gui/DataProvider.cs \ PyBinding.Gui/PythonEditorCompletion.cs \ PyBinding.Gui/PythonEditorIndentation.cs \ PyBinding.Gui/PythonEditorOutline.cs \ diff --git a/extras/PyBinding/PyBinding/PyBinding.Gui/CompilationUnitDataProvider.cs b/extras/PyBinding/PyBinding/PyBinding.Gui/CompilationUnitDataProvider.cs new file mode 100644 index 0000000000..d99f8b2755 --- /dev/null +++ b/extras/PyBinding/PyBinding/PyBinding.Gui/CompilationUnitDataProvider.cs @@ -0,0 +1,98 @@ +// +// CompilationUnitDataProvider.cs +// +// Author: +// Levi Bard <taktaktaktaktaktaktaktaktaktak@gmail.com> +// +// Copyright (c) 2010 Levi Bard +// +// This source code is licenced under The MIT License: +// +// 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; +using MonoDevelop.Ide.Gui; +using MonoDevelop.Ide.Gui.Content; +using MonoDevelop.Ide.CodeCompletion; +using MonoDevelop.Components; +using MonoDevelop.Projects.Dom; + +using Gtk; + +namespace PyBinding +{ + // Yoinked from C# binding + public class CompilationUnitDataProvider : DropDownBoxListWindow.IListDataProvider + { + Document Document { get; set; } + + public CompilationUnitDataProvider (Document document) + { + this.Document = document; + } + + #region IListDataProvider implementation + public void Reset () { } + + public string GetMarkup (int n) + { + return Document.ParsedDocument.UserRegions.ElementAt (n).Name; + } + + internal static Gdk.Pixbuf Pixbuf + { + get { return ImageService.GetPixbuf (Gtk.Stock.Add, IconSize.Menu); } + } + + public Gdk.Pixbuf GetIcon (int n) + { + return Pixbuf; + } + + public object GetTag (int n) + { + return Document.ParsedDocument.UserRegions.ElementAt (n); + } + + + public void ActivateItem (int n) + { + var reg = Document.ParsedDocument.UserRegions.ElementAt (n); + MonoDevelop.Ide.Gui.Content.IExtensibleTextEditor extEditor = Document.GetContent<MonoDevelop.Ide.Gui.Content.IExtensibleTextEditor> (); + if (extEditor != null) + extEditor.SetCaretTo (Math.Max (1, reg.Region.Start.Line), reg.Region.Start.Column); + } + + public int IconCount + { + get { + if (Document.ParsedDocument == null) + return 0; + return Document.ParsedDocument.UserRegions.Count (); + } + } + + #endregion + } +} + diff --git a/extras/PyBinding/PyBinding/PyBinding.Gui/DataProvider.cs b/extras/PyBinding/PyBinding/PyBinding.Gui/DataProvider.cs new file mode 100644 index 0000000000..66b572d983 --- /dev/null +++ b/extras/PyBinding/PyBinding/PyBinding.Gui/DataProvider.cs @@ -0,0 +1,118 @@ +// +// DataProvider.cs +// +// Author: +// Levi Bard <taktaktaktaktaktaktaktaktaktak@gmail.com> +// +// Copyright (c) 2010 Levi Bard +// +// This source code is licenced under The MIT License: +// +// 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.Collections.Generic; + +using MonoDevelop.Ide; +using MonoDevelop.Ide.Gui; +using MonoDevelop.Ide.Gui.Content; +using MonoDevelop.Ide.CodeCompletion; +using MonoDevelop.Components; +using MonoDevelop.Projects.Dom; +using MonoDevelop.Projects.Dom.Output; + +using Gtk; + +namespace PyBinding +{ + // Yoinked from C# binding + public class DataProvider : DropDownBoxListWindow.IListDataProvider + { + object tag; + Ambience amb; + List<IMember> memberList = new List<IMember> (); + + Document Document { get; set; } + + public DataProvider (Document doc, object tag, Ambience amb) + { + this.Document = doc; + this.tag = ((INode)tag).Parent; + this.amb = amb; + Reset (); + } + + #region IListDataProvider implementation + public void Reset () + { + memberList.Clear (); + if (tag is ICompilationUnit) { + Stack<IType> types = new Stack<IType> (((ICompilationUnit)tag).Types); + while (types.Count > 0) { + IType type = types.Pop (); + memberList.Add (type); + foreach (IType innerType in type.InnerTypes) + types.Push (innerType); + } + } else if (tag is IType) { + memberList.AddRange (((IType)tag).Members); + } + memberList.Sort ((x, y) => String.Compare (GetString (amb, x), GetString (amb, y), StringComparison.OrdinalIgnoreCase)); + } + + string GetString (Ambience amb, IMember x) + { + if (tag is ICompilationUnit) + return amb.GetString (x, OutputFlags.IncludeGenerics | OutputFlags.IncludeParameters | OutputFlags.UseFullInnerTypeName | OutputFlags.ReformatDelegates); + return amb.GetString (x, OutputFlags.IncludeGenerics | OutputFlags.IncludeParameters | OutputFlags.ReformatDelegates); + } + + public string GetMarkup (int n) + { + return GetString (amb, memberList[n]); + } + + public Gdk.Pixbuf GetIcon (int n) + { + return ImageService.GetPixbuf (memberList[n].StockIcon, IconSize.Menu); + } + + public object GetTag (int n) + { + return memberList[n]; + } + + public void ActivateItem (int n) + { + var member = memberList[n]; + MonoDevelop.Ide.Gui.Content.IExtensibleTextEditor extEditor = Document.GetContent<MonoDevelop.Ide.Gui.Content.IExtensibleTextEditor> (); + if (extEditor != null) + extEditor.SetCaretTo (Math.Max (1, member.Location.Line), Math.Max (1, member.Location.Column)); + } + + public int IconCount { + get { + return memberList.Count; + } + } + #endregion + } +} + diff --git a/extras/PyBinding/PyBinding/PyBinding.Gui/PythonEditorCompletion.cs b/extras/PyBinding/PyBinding/PyBinding.Gui/PythonEditorCompletion.cs index 512fe01344..e09128beb8 100644 --- a/extras/PyBinding/PyBinding/PyBinding.Gui/PythonEditorCompletion.cs +++ b/extras/PyBinding/PyBinding/PyBinding.Gui/PythonEditorCompletion.cs @@ -36,10 +36,13 @@ using MonoDevelop.Ide.CodeCompletion; using PyBinding; using PyBinding.Parser; using PyBinding.Parser.Dom; +using MonoDevelop.Components; +using MonoDevelop.Projects.Dom.Output; +using MonoDevelop.Ide; namespace PyBinding.Gui { - public class PythonEditorCompletion : CompletionTextEditorExtension + public class PythonEditorCompletion : CompletionTextEditorExtension, IPathedDocument { const string s_ImgModule = "md-package"; const string s_ImgAttr = "md-property"; @@ -60,6 +63,10 @@ namespace PyBinding.Gui if (m_site == null) m_site = new PythonSite (PythonHelper.FindPreferedRuntime ()); + + UpdatePath (null, null); + Document.Editor.Caret.PositionChanged += UpdatePath; + Document.DocumentParsed += delegate { UpdatePath (null, null); }; } public override ICompletionDataList HandleCodeCompletion (CodeCompletionContext completionContext, char completionChar) @@ -263,5 +270,109 @@ namespace PyBinding.Gui return line; } + + #region IPathedDocument implementation + public event EventHandler<DocumentPathChangedEventArgs> PathChanged; + + public Gtk.Widget CreatePathWidget (int index) + { + PathEntry[] path = CurrentPath; + if (null == path || 0 > index || path.Length <= index) { + return null; + } + + object tag = path[index].Tag; + DropDownBoxListWindow.IListDataProvider provider = null; + if (tag is ICompilationUnit) { + provider = new CompilationUnitDataProvider (Document); + } else { + provider = new DataProvider (Document, tag, GetAmbience ()); + } + + DropDownBoxListWindow window = new DropDownBoxListWindow (provider); + window.SelectItem (tag); + return window; + } + + public PathEntry[] CurrentPath { + get; + private set; + } + + protected virtual void OnPathChanged (object sender, DocumentPathChangedEventArgs args) + { + if (PathChanged != null) + PathChanged (sender, args); + } + #endregion + + // Yoinked from C# binding + void UpdatePath (object sender, Mono.TextEditor.DocumentLocationEventArgs e) + { + var unit = Document.CompilationUnit; + var textEditorData = Document.Editor; + + if (unit == null) + return; + + var loc = textEditorData.Caret.Location; + IType type = unit.GetTypeAt (loc.Line, loc.Column); + List<PathEntry> result = new List<PathEntry> (); + Ambience amb = GetAmbience (); + IMember member = null; + INode node = (INode)unit; + + if (type != null && type.ClassType != ClassType.Delegate) { + member = type.GetMemberAt (loc.Line, loc.Column); + } + + if (null != member) { + node = member; + } else if (null != type) { + node = type; + } + + while (node != null) { + PathEntry entry; + if (node is ICompilationUnit) { + if (!Document.ParsedDocument.UserRegions.Any ()) + break; + FoldingRegion reg = Document.ParsedDocument.UserRegions.Where (r => r.Region.Contains (loc.Line, loc.Column)).LastOrDefault (); + if (reg == null) { + entry = new PathEntry (GettextCatalog.GetString ("No region")); + } else { + entry = new PathEntry (CompilationUnitDataProvider.Pixbuf, reg.Name); + } + entry.Position = EntryPosition.Right; + } else { + entry = new PathEntry (ImageService.GetPixbuf (((IMember)node).StockIcon, Gtk.IconSize.Menu), amb.GetString ((IMember)node, OutputFlags.IncludeGenerics | OutputFlags.IncludeParameters | OutputFlags.ReformatDelegates)); + } + entry.Tag = node; + result.Insert (0, entry); + node = node.Parent; + } + + PathEntry noSelection = null; + if (type == null) { + noSelection = new PathEntry (GettextCatalog.GetString ("No selection")) { Tag = new CustomNode (Document.CompilationUnit) }; + } else if (member == null && type.ClassType != ClassType.Delegate) + noSelection = new PathEntry (GettextCatalog.GetString ("No selection")) { Tag = new CustomNode (type) }; + if (noSelection != null) { + result.Add (noSelection); + } + + var prev = CurrentPath; + CurrentPath = result.ToArray (); + OnPathChanged (this, new DocumentPathChangedEventArgs (prev)); + } + + // Yoinked from C# binding + class CustomNode : MonoDevelop.Projects.Dom.AbstractNode + { + public CustomNode (INode parent) + { + this.Parent = parent; + } + } } } diff --git a/extras/PyBinding/PyBinding/PyBinding.csproj b/extras/PyBinding/PyBinding/PyBinding.csproj index 364fc4382e..6d67e7f82d 100644 --- a/extras/PyBinding/PyBinding/PyBinding.csproj +++ b/extras/PyBinding/PyBinding/PyBinding.csproj @@ -174,6 +174,8 @@ <Compile Include="PyBinding.Gui\PythonEditorIndentation.cs" /> <Compile Include="PyBinding.Gui\PythonEditorOutline.cs" /> <Compile Include="PyBinding.Gui\PythonOptionsPanel.cs" /> + <Compile Include="PyBinding.Gui\DataProvider.cs" /> + <Compile Include="PyBinding.Gui\CompilationUnitDataProvider.cs" /> <Compile Include="PyBinding\PythonExecutionCommand.cs" /> <Compile Include="PyBinding\PythonExecutionHandler.cs" /> <Compile Include="PyBinding\PythonSite.cs" /> |