Skip to content

Commit

Permalink
Initial jk support for search results pad. Re #193
Browse files Browse the repository at this point in the history
  • Loading branch information
nosami committed Apr 29, 2018
1 parent e0c723c commit 0a77f75
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
23 changes: 21 additions & 2 deletions XSVim/Addin.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ open System
open System.Collections.Generic
open MonoDevelop.Components.Commands
open MonoDevelop.Core
open MonoDevelop.Core.Text
open MonoDevelop.Ide
open MonoDevelop.Ide.Editor
open MonoDevelop.Ide.Editor.Extension
open MonoDevelop.Ide.FindInFiles

type XSVim() =
inherit TextEditorExtension()
let mutable disposables : IDisposable list = []
let mutable processingKey = false
let mutable config = { insertModeEscapeKey = None }

let searchPads = HashSet<string>()
let initConfig() =
let mapping = SettingsPanel.InsertModeEscapeMapping()
if mapping.Length = 2 then
Expand All @@ -26,6 +26,24 @@ type XSVim() =
else
config <- { insertModeEscapeKey = None }

let initializeSearchResultsPads() =
IdeApp.Workbench
|> Option.ofObj
|> Option.iter(fun workbench ->
workbench.Pads
|> Seq.iter(fun pad ->
try
// fetching pad.Content can throw when there is an exception
// when initializing the pad
tryUnbox<SearchResultPad> pad.Content
|> Option.iter(fun pad ->
let padId = pad.Window.Id
if not (searchPads.Contains padId) then
searchPads.Add padId |> ignore
searchResultsPad.initialize pad)
with
| _ -> ()))

member x.FileName = x.Editor.FileName.FullPath.ToString()

member x.State
Expand All @@ -34,6 +52,7 @@ type XSVim() =

override x.Initialize() =
treeViewPads.initialize()
x.Editor.FocusLost.Add(fun _ -> initializeSearchResultsPads())

initConfig()
if not (Vim.editorStates.ContainsKey x.FileName) then
Expand Down
2 changes: 1 addition & 1 deletion XSVim/Properties/AssemblyInfo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open System.Runtime.CompilerServices
[<AutoOpen>]
module AddinVersion =
[<Literal>]
let version = "0.53.1"
let version = "0.54.1.1"

[<assembly: AssemblyTitle("XSVim")>]
// The assembly version has the format {Major}.{Minor}.{Build}.{Revision}
Expand Down
33 changes: 20 additions & 13 deletions XSVim/Reflection.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ module Reflection =

// Static member call (on value of type System.Type)?
if (typeof<System.Type>).IsAssignableFrom(o.GetType()) then
let methods = (unbox<Type> o).GetMethods(staticFlags) |> Array.map asMethodBase
let ctors = (unbox<Type> o).GetConstructors(ctorFlags) |> Array.map asMethodBase
Array.concat [ methods; ctors ], null, args
let methods = (unbox<Type> o).GetMethods(staticFlags) |> Array.map asMethodBase
let ctors = (unbox<Type> o).GetConstructors(ctorFlags) |> Array.map asMethodBase
Array.concat [ methods; ctors ], null, args
else
o.GetType().GetMethods(instanceFlags) |> Array.map asMethodBase, o, args

Expand All @@ -60,8 +60,8 @@ module Reflection =
// The result type is not an F# function, so we're getting a property
// When the 'o' object is 'System.Type', we access static properties
let typ, flags, instance =
if (typeof<System.Type>).IsAssignableFrom(o.GetType())
then unbox o, staticFlags, null
if (typeof<System.Type>).IsAssignableFrom(o.GetType())
then unbox o, staticFlags, null
else o.GetType(), instanceFlags, o

// Find a property that we can call and get the value
Expand All @@ -71,17 +71,24 @@ module Reflection =
let nested = typ.Assembly.GetType(typ.FullName + "+" + name)
// Return nested type if we found one
if nested = null then
failwithf "Property or nested type '%s' not found in '%s'." name typ.Name
failwithf "Property nested type '%s' not found in '%s'." name typ.Name
elif not ((typeof<'R>).IsAssignableFrom(typeof<System.Type>)) then
let rname = (typeof<'R>.Name)
failwithf "Cannot return nested type '%s' as a type '%s'." nested.Name rname
let rname = (typeof<'R>.Name)
failwithf "Cannot return nested type '%s' as a type '%s'." nested.Name rname
else nested |> box |> unbox<'R>
else
// Call property and return result if we found some
let meth = prop.GetGetMethod(true)
if prop = null then failwithf "Property '%s' found, but doesn't have 'get' method." name
try meth.Invoke(instance, [| |]) |> unbox<'R>
with _ -> failwithf "Failed to get value of '%s' property (of type '%s')" name typ.Name
if prop = null then
// if we didn't find a nested type then search for a field
let field = typ.GetField(name, flags)
if not (isNull field) then
field.GetValue(o) |> unbox<'R>
else
failwithf "Property '%s' found, but doesn't have 'get' method." name
else
// Call property and return result if we found some
let meth = prop.GetGetMethod(true)
try meth.Invoke(instance, [| |]) |> unbox<'R>
with _ -> failwithf "Failed to get value of '%s' property (of type '%s')" name typ.Name

let (?<-) (this:obj) (property:string) (value:'Value) =
this.GetType().GetProperty(property).SetValue(this, value, null)
42 changes: 42 additions & 0 deletions XSVim/SearchResultsPad.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace XSVim
open System
open Gtk
open MonoDevelop.Ide.FindInFiles
open MonoDevelop.Ide.Gui.Components
open Reflection

module searchResultsPad =
let select (tree:TreeView) path =
let column = tree.Columns.[0]
tree.Selection.SelectPath path
tree.SetCursor(path, column, false)

let getSelectedPath (tree:TreeView) =
tree.Selection.GetSelectedRows().[0]

let moveDown tree =
let path = getSelectedPath tree
path.Next()
select tree path

let moveUp tree =
let path = getSelectedPath tree
path.Prev() |> ignore
select tree path

let initialize (pad:SearchResultPad) =
let tree:PadTreeView = pad.Control?nativeWidget?treeviewSearchResults
let processKey (key:KeyPressEventArgs) =
match key.Event.Key with
| Gdk.Key.Escape ->
dispatchCommand "MonoDevelop.Ide.Commands.ViewCommands.FocusCurrentDocument"
key.RetVal <- false
| Gdk.Key.j ->
moveDown tree
key.RetVal <- true
| Gdk.Key.k ->
moveUp tree
key.RetVal <- true
| _ -> ()

tree.KeyPressEvent.Add processKey
1 change: 1 addition & 0 deletions XSVim/XSVim.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<Compile Include="Types.fs" />
<Compile Include="WindowManagement.fs" />
<Compile Include="ExMode.fs" />
<Compile Include="SearchResultsPad.fs" />
<Compile Include="TreeViewPads.fs" />
<Compile Include="XSVim.fs" />
<Compile Include="Addin.fs" />
Expand Down

0 comments on commit 0a77f75

Please sign in to comment.