Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Add command to extract method out of selected code #370

Merged
merged 12 commits into from
Jul 11, 2016

Conversation

ramya-rao-a
Copy link
Contributor

Related to a feature ask in #275
This commit adds a command to the command palette that can be used to extract a method out of selected code and replace the selected code with a call to the extracted method using godoctor. Read more about godoctor at http://gorefactor.org/

@msftclas
Copy link

Hi @ramya-rao-a, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!


It looks like you're a Microsoft contributor (Ramya Achutha Rao). If you're full-time, we DON'T require a Contribution License Agreement. If you are a vendor, please DO sign the electronic Contribution License Agreement. It will take 2 minutes and there's no faxing! https://cla.microsoft.com.

TTYL, MSBOT;

@mattetti
Copy link
Contributor

mattetti commented Jul 8, 2016

This is a cool feature/tool @ramya-rao-a! Would you mind checking why the build is failing? It might also be nice to add this feature to the context menu now that it's available to extensions.

@mattetti
Copy link
Contributor

mattetti commented Jul 8, 2016

Looks like something is wrong with the cert used by 9fans.net used by github.com/rogpeppe/godef:

import "9fans.net/go/acme": https fetch: Get https://9fans.net/go/acme?go-get=1: x509: certificate has expired or is not yet valid

@ramya-rao-a
Copy link
Contributor Author

ramya-rao-a commented Jul 8, 2016

Thanks @mattetti . Once I get this PR merged, I'll get to the context menu.
Initially build failed as go get godoctor failed with Go v.1.5
setting GO15VENDOREXPERIMENT=1 fixes that.

Need to set the same via travis

return editPromise.then(done => {
if (done && !editor.document.getText().endsWith('\n')) {
return editor.edit((editBuilder) => {
editBuilder.insert(new Position(editor.document.lineCount, 0), '\n');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the \n if necessary on line 74? Also this should probably insert the same line endings as are used in the document (\r\n or \n).

process.stdin.end(editor.document.getText() + '\n');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tyriar Extracted methods get added at the end of the document. If \n gets added to the input, then the resulting diffs/patches will assume this \n exists and not output one. This will result in the extracted method getting appended to the last line instead of starting from a new line after the last line.

@mattetti
Copy link
Contributor

mattetti commented Jul 9, 2016

I'll be doing a remote meetup during GopherCon this week and will demo VS Code, I'd love to show off this feature :)

@egamma
Copy link
Member

egamma commented Jul 11, 2016

I've looked at the code and it LGTM.

@mattetti we are having some struggle with the publishing of this extension. For the meetup can you try to side-load the extension?

  • make sure you have vsce installed globally: npm install -g vsce
  • git clone/pull
  • npm install
  • vsce package
  • code Go-xxx.vsix <- the vsix created by the step before

@Tyriar Tyriar merged commit 2713ab7 into microsoft:master Jul 11, 2016
@mattetti
Copy link
Contributor

@egamma it turned out the internet connection at the conference wasn't good enough for the live streaming so we postponed the event. I however showed off VS Code to many people, especially because there was a talk on Guru (used by VS Code) and Delve (the debugger used by Code). Lots of people were very impressed by how fast and feature full VS Code for Go is.

I'll side-load master and show the refactoring function tomorrow during the hack day.

Thanks a lot

@egamma
Copy link
Member

egamma commented Jul 13, 2016

@mattetti thanks for the update and for sharing vscode-go with the Go developers.

Note to others finding this issue, you need to have vsce installed globally to side load the extension:
npm install -g vsce

Thanks, I've updated the instructions above.

@mattetti
Copy link
Contributor

One small detail: make sure to restart Code (I forgot the first time around ;))

A few things about this new feature, while it's the promise of this refactoring tool is really exciting, the practical use case seems to be pretty limited. I couldn't extract code with return statements, I saw weird errors of undefined types (no idea what was going on) and finally this interesting error: Error: Completing the transformation will introduce the following error: <stdin>:201:2: no new variables on left side of :=

The error is interesting because it's meaningless for the user, I have not idea what code is on line 201 of the stdin buffer, and no idea why it ended being an error. My guess is that my extracted code was added as a function but the new code wasn't valid (it needed some massaging?), so the underlying tool failed with this cryptic error.

@ramya-rao-a did you have good luck using this refactoring tool? It's highly possible I got very unlucky and the code I was trying to refactor wasn't a good example.

@lukehoban
Copy link
Contributor

I just had a chance to try out this feature. In the 4 projects I tried it in, and half dozen locations trying to extract method in each, I didn't actually find a single case where it worked :-).

Are we sure that GoDoctor is robust enough for this integration, and if so, that the integration is working sufficiently often that this feature is a net win for the extension?

Here are a few of the errors I saw during an Extract Method:

  • Failed to parse the patches from godoctor: Illegal escape in patch_fromText: default:
  • Error: Completing the transformation will introduce the following error: <stdin>:59:2: no new variables on left side of :=
  • Error: could not import golang.org/x/net/http2/hpack (cannot find package "golang.org/x/net/http2/hpack" in any of:
  • Error: Code containing return statements may change behavior if it is extracted.

I also agree with @mattetti that the last of these makes this hard to use in practice in Go, since return statements are so common in error checking interspersed through Go function bodies.

Also, in most of the projects I tried, these changes appear to have broken formatting with Cannot read property 'apply' of undefined. And format-on-save, which is probably the most used feature of the extension, doesn't appear to get triggered at all anymore (perhaps the error above is causing that?).

The issues with format will need to get fixed before we can push an update for the extension, and I suspect it may be best to revert this feature from master until it is more polished such that it works a reasonable percent of the time that users try to use it.

@mattetti
Copy link
Contributor

I talked with my friend Fatih who develops and maintains go-vim. He told me
he chose to not integrate this tool on purpose, apparently because it
doesn't seem to work quite right and he would have to deal with support.

I am excited about the opportunity of having a great refactoring/function
extractor but I would suggest to wait until we have a more solid solution.

On Sat, Jul 16, 2016, 12:01 Luke Hoban notifications@github.com wrote:

I just had a chance to try out this feature. In the 4 projects I tried it
in, and half dozen locations trying to extract method in each, I didn't
actually find a single case where it worked :-).

Are we sure that GoDoctor is robust enough for this integration, and if
so, that the integration is working sufficiently often that this feature is
a net win for the extension?

Here are a few of the errors I saw during an Extract Method:

  • Failed to parse the patches from godoctor: Illegal escape in
    patch_fromText: default:
  • Error: Completing the transformation will introduce the following
    error: :59:2: no new variables on left side of :=
  • Error: could not import golang.org/x/net/http2/hpack (cannot find
    package "golang.org/x/net/http2/hpack" in any of:
  • Error: Code containing return statements may change behavior if it
    is extracted.

I also agree with @mattetti https://github.com/mattetti that the last
of these makes this hard to use in practice in Go, since return statements
are so common in error checking interspersed through Go function bodies.

Also, in most of the projects I tried, these changes appear to have broken
formatting with Cannot read property 'apply' of undefined. And
format-on-save, which is probably the most used feature of the extension,
doesn't appear to get triggered at all anymore (perhaps the error above is
causing that?).

The issues with format will need to get fixed before we can push an
update for the extension, and I suspect it may be best to revert this
feature from master until it is more polished such that it works a
reasonable percent of the time that users try to use it.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#370 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAAcWVZ8XzCx-QiSFjf4b9vQZqtLTAhks5qWSqfgaJpZM4JCo2Y
.

@ramya-rao-a
Copy link
Contributor Author

ramya-rao-a commented Jul 18, 2016

godoctor does have limitations in the extract method feature.

Code containing return statements, defer statements, or anonymous functions cannot be extracted

@mattetti My examples did not contain any of the above and the refactoring did work.

@lukehoban I didn't have format on save set, so couldnt catch the Cannot read property 'apply' of undefined. The code that applies the diffs has been extracted out of goFormat into utils. There was a missing check on null which broke the format feature which can be easily fixed.

Regardless, since it seems like godoctor doesn't extract the method well in many common scenarios like you both have experienced, I will revert the changes for now.

@lukehoban
Copy link
Contributor

Thanks @ramya-rao-a, and sorry for not reviewing this sooner!

It would be great to reopen this PR with the fixes for formatting so that folks can try out the Extract Method feature and provide feedback that may help inform whether GoDoctor could be further improved to be a useful extension.

@ramya-rao-a
Copy link
Contributor Author

@lukehoban I have reopened the PR after fixing the issue with formatting. #404
I am curious about few of the errors you encountered.

Can you provide me sample go code where you found the below errors? It would help me determine if the problem is with godoctor or my integration.

  • Failed to parse the patches from godoctor: Illegal escape in patch_fromText: default:
  • Error: Completing the transformation will introduce the following error: <stdin>:59:2: no new variables on left side of :=
  • Error: could not import golang.org/x/net/http2/hpack (cannot find package "golang.org/x/net/http2/hpack" in any of:

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants