Go To Implementation with View Call Hierarchy

Hands down, my favorite thing about ReSharper is that it adds Go To Implementation to the context menu for easy navigation to a function in a class that implements an interface. That’s kind of a wordy statement, so let me rephrase. Consider the example below. If I have a variable foo of type IFoo, ReSharper lets me right-click a call to foo.Bar() and jump directly to the implementation in either BasicFoo or ExtremeFoo.

interface IFoo
{
    public void Bar();
}

class BasicFoo : IFoo
{
    public void Bar()
    {
        Console.WriteLine("BasicFoo.Bar()");
    }
}

class ExtremeFoo : IFoo
{
    public void Bar()
    {
        Console.WriteLine("ExtremeFoo.Bar()");
    }
}

But what if you don’t have ReSharper? I used to think the next best option was Find All References, which does a good job of finding all the right places where you might want to look for an implementation. However, the implementations are mixed with the method itself and calls to the method, and the results returned by Find All References can become rather unruly.

find-all-references

Note that I said “used to think,” though. See that option under Find All References in the context menu, View Call Hierarchy? Use that. If you look at the Call Hierarchy for an interface method, you’ll get a group of implementations–exactly what I love about ReSharper’s Go To Implementation! You’ll also have a group of callers to the method which is what you get from another ReSharper nicety, Find Usages. Awesome.

call-hierarchy

Call Hierarchy is pretty amazing, and it’s kind of a bummer that I didn’t know about it until just now. Find All References still has its uses, but it’s taking a backseat to View Call Hierarchy in my toolkit. If you haven’t used it before, give it a shot–I bet you’ll love it, too.

Retrieving Properties from PSObject ExtensionData Using Reflection

Oh, PowerShell. Why do you do this to me? I run a query from your command line and see a bounty of properties, But, alas, when run from code, the properties that I expect are missing! Where could they be? ExtensionData? Ugh.

Now, it really seems like getting property values out of ExtensionData shouldn’t be so hard. It was difficult and annoying enough to me that I feel like I must be doing something wrong. If you know of a better way, please–PLEASE–let me know. Until then, I present to you a method for accessing the properties using reflection.

In this example, I’m essentially trying to access the following path of a PSObject:

psObject.ExtensionData.Value.Members
    .FirstOrDefault(x => x.Name == "propertyName").Value.Value

Things start off pretty straightforward. We want to get the ExtensionData property, which is included in the PSObject‘s Members collection.

var extensionData = psObject.Members["ExtensionData"].Value;

extensionData has its own Members collection, so we get that next. It’s not a public property, though, so we have to dig it out using reflection. Also note that we cast the object to an IEnumerable<object>

var members = extensionData.GetType()
    .GetProperty("Members", BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(extensionData) as IEnumerable<object>;

Things are starting to get a little trickier. We need to reflect on the members to find the property name that we’re looking for.

var memberType = members.First().GetType();
var nameProperty = memberType.Getproperty("Name", BindingFlags.Public | BindingFlags.Instance);
var member = members
    .Where(x => string.equals(propertyName, nameProperty.GetValue(x) as string, 
        StringComparison.InvariantCultureIgnoreCase))
    .FirstOrDefault();

Now we’re in the home stretch, and we just need to get the property value. One caveat, though: the property is a data node, so you actually need to get its value. That’s right, we need Value.Value.

var valueProperty = memberType.GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
var value = valueProperty.GetValue(member);
valueProperty = value.GetType().GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
return valueProperty.GetValue(value) as string;

It got kinda gross in the end there, but mission accomplished. I found that the data types weren’t preserved in the extension data, so I had to return values as strings and cast to the appropriate data type (e.g., bool) outside my function.

Here’s the complete solution. (Error/null-checks omitted for brevity.)

string GetPropertyFromExtensionData(PSObject psObject, string propertyName)
{
    var extensionData = psObject.Members["ExtensionData"].Value;

    // members = extensionData.Members as IEnumerable<object>
    var members = extensionData.GetType()
        .GetProperty("Members", BindingFlags.NonPublic | BindingFlags.Instance)
        .GetValue(extensionData) as IEnumerable<object>;

    // member = members.Where(x => x.Name == propertyName)
    var memberType = members.First().GetType();
    var nameProperty = memberType.Getproperty("Name", BindingFlags.Public | BindingFlags.Instance);
    var member = members
        .Where(x => string.equals(propertyName, nameProperty.GetValue(x) as string, 
            StringComparison.InvariantCultureIgnoreCase))
        .FirstOrDefault();

    // return member.Value.Value as string
    var valueProperty = memberType.GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
    var value = valueProperty.GetValue(member);
    valueProperty = value.GetType().GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
    return valueProperty.GetValue(value) as string;
}

Visual Studio Fix: Intellisense Not Popping Up or Not Working « OmegaMan’s Musings

I was monkeying around with Resharper optimization and lost Intellisense altogether! Here’s how I got it back.

In quick launch type “Statement Completion” and choose the first one  “Text Editor -> All Languages -> General” to be taken to the options page for all languages.

On the general options page in the “Statement Completion” section override whatever is in the boxes “Auto list members” and “Parameter Information” and makes those checked. Such as shown here:

via Visual Studio Fix: Intellisense Not Popping Up or Not Working « OmegaMan’s Musings.

A caution about git branch names with ‘/’s

Git, you tricky bastard.

Let’s say I first create a branch named ‘wip/foo':

git branch wip/foo

Git will create a file named ‘foo’ in the folder .git/refs/head/wip/. Now, suppose I attempt to create a new branch named ‘wip/foo/foo-offshoot’. git will return an error:

error: unable to create directory for .git/refs/heads/wip/foo/foo-offshoot
fatal: Failed to lock ref for update: No such file or directory

via coderwall.com : establishing geek cred since 1305712800.

Introducing Quick File Search | Bitbucket Blog

I’m glad I found this, I’ve really been needing/wanting it. In Bitbucket, you can press ‘F’ to bring up a quick-search dialog and type in any file name to find & jump directly to a file in the repository. Until now, I’ve been manually clicking through directories and hating every second of it! (But this is apparently old news. Using ‘.’ to open the Omnibar is the way to go now. Exact same idea, though!)

Introducing Quick File Search | Bitbucket Blog.

Get Your Pyth-on with PyCharm

I’m still a relative noob when it comes to Python. I started just a few months back and have mostly dabbled with website automation using Selenium. I started by just using Notepad++ and quickly graduated to gVim, but I was missing out on a lot of common IDE features. I wanted to be able to set breakpoints and jump to functions in a go-to-definition fashion.

So I did some searching and found PyCharm from JetBrains. It comes in two flavors, professional and community. Professional offers a 30-day free trial, then you have to buy whereas the community edition is free to use. Since I’m only interested in lightweight, Python-only development, the community edition is perfect for me.

pycharm

The things I like best about PyCharm are exactly what I mentioned above. I can right-click a function or variable and jump to its definition. I can set breakpoints for debugging, or even just run the script and see results in the built-in console. Code completion and code analysis are also extremely helpful since I’m still pretty new. Being able to scroll through a list of available functions on an object is much more efficient than searching the internet each time, and code analysis helps me learn about the conventions of this new language.

It comes with a variety of keymaps to choose from. Since I was doing my previous Python development in gVim, I installed the Vim plug-in. I soon grew tired of re-learning the ins & outs of Vim, though, and switched to the Visual Studio keymap. A few adjustments later (Declaration to F12, Back to Ctrl+Minus, & Forward to Ctrl+Shift+Minus) and I felt right at home.

pycharm-keymap

I was happy to see that you can create and manage virtual environments through the IDE, allowing projects to be isolated and free from the unneeded packages of other projects.

pycharm-virtualenv

Another great feature is that it automatically detected and integrated with source control (Git). Without any configuration at all, I’m able to view the history of a file and compare versions.

pycharm-git

PyCharm gives me that full IDE feel that I’m used to and was looking for. I definitely recommend it for anybody getting started with Python. I think I’d even suggest checking it out to grizzled vets.

git – How to list branches that contain a given commit?

Need to see which branch(es) contain a Git commit? Try these commands!

List local branches that contain a commit:

git branch --contains <commit>

List remote branches that contain a commit:

git branch -r --contains <commit>

List all (local & remote) branches that a commit:

git branch -a --contains <commit>

via git – How to list branches that contain a given commit? – Stack Overflow.

Follow

Get every new post delivered to your Inbox.

Join 44 other followers

%d bloggers like this: