Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omnisharp and Unity3D #152

Closed
HiPhish opened this issue Aug 23, 2014 · 41 comments
Closed

Omnisharp and Unity3D #152

HiPhish opened this issue Aug 23, 2014 · 41 comments

Comments

@HiPhish
Copy link

HiPhish commented Aug 23, 2014

I have been trying to get Omnisharp to work with the Unity3D Game Engine but without success.

I installed Omnisharp using Vundle, as well as vim-dispatch, syntastic, Ctrl-P, Supertab and YouCompleteMe. As far as I understand Vundle does the whole git submodule update --init --recursive thing for me.

I have set up a blank new project for testing and created a new C# script. This is what a script looks like by default:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
}

I set my external editor to MacVim and then went to Assets -> Sync Monodevelop Project as described here:
http://docs.unity3d.com/Manual/VisualStudioIntegration.html

A new MacVim window opens with the solution file loaded (New Unity Project.sln) and I can double-click my script. however, code completion does not recognise any patters that aren't in the file already. For example, typing vec or v3 will not offer Vector3 until i have already written it somewhere in my file. There might be more things not working, but this is what i noticed at first.

I will provide any assistance I can to solving this issue, every minute I have to spend in Unity's MonoDevelop or Xamarin Studio is driving me nuts. Thank you for this plugin, hopefully we can get it to work with Unity as well.

@nosami
Copy link
Contributor

nosami commented Aug 23, 2014

Stupid question but did you compile the server?

@nosami
Copy link
Contributor

nosami commented Aug 23, 2014

OmniSharp certainly works with Unity. I think Unity devs comprise the largest part of the OmniSharp user base.

@HiPhish
Copy link
Author

HiPhish commented Aug 23, 2014

I ran xbuild from OmniSharp's server directory (~/.vim/bundle/Omnisharp/server) if that's wht you mean by compiling the server; I got 8 warnings but no errors. is there a way I can test if my server is build correctly?

If OmniSharp is known to work with Unity the error might be on my side. Is it OK if we keep discussing it here in the issue tracker?

@nosami
Copy link
Contributor

nosami commented Aug 23, 2014

If you got no errors, then yes, it is built correctly.

Do other OmniSharp features work - goto definition etc? Are you using other plugins to trigger completions? You haven't given me a lot to work with here :)

@HiPhish
Copy link
Author

HiPhish commented Aug 24, 2014

Well, you haven't given me a lot to work with either, I can't find any user manual for OmniSharp :) I don't know how to use goto definition and the likes. And no, I don't have any plugins installed aside from the above. Here is what my .vimrc looks like:

set nocompatible
filetype off 
syntax on
set number

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle (required)
Plugin 'gmarik/Vundle.vim'

Plugin 'tpope/vim-dispatch'
Plugin 'scrooloose/syntastic'
Plugin 'kien/ctrlp.vim'
Plugin 'ervandew/supertab'
Plugin 'Valloric/YouCompleteMe'
Plugin 'nosami/Omnisharp'

" All of your plugins must be added before this line
call vundle#end()
filetype plugin indent on

@nosami
Copy link
Contributor

nosami commented Aug 24, 2014

There isn't a manual. All the instructions are on the README. Luckily for me, this has been quite adequate up until now - this is the first such question that I've had.

Having said that, I can see from your vimrc file that you are new to Vim :) You will need some keybindings unless you want to invoke OmniSharp functions directly. You can find some example keybindings in the README.

Also, please double check that the server is running with

ps ax | grep OmniSharp.exe

@HiPhish
Copy link
Author

HiPhish commented Aug 24, 2014

Yeah, I know I will need key bindings, but I wanted to make sure things work before I start thinking about key bindings. Here is what I get printed (I assume I was supposed to call that command from Vim)

:! ps ax | grep OmniSharp.exe
14279   p0  Ss+    0:00.00 /bin/bash -c  ps ax | grep OmniSharp.exe
14281   p0  S+     0:00.00 grep OmniSharp.exe

Also, sometimes when i open a C# script I get the following asked:

"GFHexGrid.cs" 1218L, 55383C
sln:
1 - /Users/hiphish/Unity Projects/Grid Workshop/Grid Workshop-csharp.sln
2 - /Users/hiphish/Unity Projects/Grid Workshop/Grid Workshop.sln
Choose a solution file

Which one should I pick?

@nosami
Copy link
Contributor

nosami commented Aug 24, 2014

The reason I said you will need some keybindings is because you stated that you didn't know how to use gotodefinition etc.

It doesn't matter whether or not you run the command from Vim or not. You should see something like

jason@Jasons-MacBook-Pro ~/.vim/bundle/Omnisharp/server (5b518ff*) $ ps ax | grep OmniSharp.exe
63556 s000  U+     0:02.47 mono /Users/jason/.vim/bundle/Omnisharp/server/OmniSharp/bin/Debug/OmniSharp.exe -p 2000 -s /Users/jason/.vim/bundle/Omnisharp/server/OmniSharp.sln
63570 s002  R+     0:00.00 grep OmniSharp.exe

when the server is running. The server isn't started until it has established which sln file you want to use. If there is only one solution file, then you wouldn't see that prompt - I guess you should pick the -csharp.sln - but not really sure ( I don't do Unity )

@nosami
Copy link
Contributor

nosami commented Aug 24, 2014

Also, I would try and get OmniSharp working without YouCompleteMe at first as that complicates things somewhat.

You'll need at a minimum

autocmd FileType cs setlocal omnifunc=OmniSharp#Complete

You may as well paste the whole of the example .vimrc into your file for now and tweak it later.

Once you've established that the server has started, try and get completions with <C-x><C-o> or configure SuperTab.

@HiPhish
Copy link
Author

HiPhish commented Aug 24, 2014

I recompiled Vim, MacVim and YCM so they all work nicely together now. I also uninstalled and re-installed all my plugins and copy-pasted your example vimrc. here is what I get when executing the above command:

:! ps ax | grep OmniSharp.exe
14302   ??  S      0:00.00 /bin/bash -c trap 'rm -f /var/folders/h6/pnstmz6d6ws9yt188f7rf4dr0000gn/T/vhm4TMR/1.pid; touch /var/folders/h6/pnstmz6d6ws9yt188f7rf4dr0000gn/T/vhm4TMR/1.complete' EXIT INT TERM; echo $$ > /var/folders/h6/pnstmz6d6ws9yt188f7rf4dr0000gn/T/vhm4TMR/1.pid; perl -e "select(undef,undef,undef,0.1)"; mono '/Users/hiphish/.vim/bundle/Omnisharp/server/OmniSharp/bin/Debug/OmniSharp.exe' -p 2000 -s '/Users/hiphish/Unity Projects/Grid Workshop/Grid Workshop-csharp.sln'
14310   ??  S      0:11.31 mono /Users/hiphish/.vim/bundle/Omnisharp/server/OmniSharp/bin/Debug/OmniSharp.exe -p 2000 -s /Users/hiphish/Unity Projects/Grid Workshop/Grid Workshop-csharp.sln
32069 s000  S+     0:00.00 /bin/bash -c  ps ax | grep OmniSharp.exe
32071 s000  U+     0:00.00 /bin/bash -c  ps ax | grep OmniSharp.exe

Here is what happens when i try to type Vector2 as v2:

  • as I type I get -- User defined completion (^U^N^P) Pattern not found all the time, even when I type random junk
  • I also keep getting a red-coloured error UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 252: ordinal not in range(128) (my terminal is set to UTF8 encoding, as is the default, if that matters). My scripts are also UTF-8-encoded (checked with file -I)
  • pressing ctrl-X prints -- ^X++ mode (/^E/^Y/^L/^N/^K/^T/^I/^]/^F/^D/^V/^P/^U/^O/s/n/p)

@vexe
Copy link

vexe commented Aug 24, 2014

v2 won't be auto-completed as Vector2 unless you have it mapped or an abbreviation. Try and type "Vec" and then Ctrl+o

I also prefer to use gvim over a regular vim - things just work better

@vexe
Copy link

vexe commented Aug 24, 2014

But I have to agree, Unity integration isn't so smooth. I Just tried it myself, I had already a server instance running - I created a new project with a sample script like the one mentioned above ^ - O# wasn't able to identify the class "MonoBehaviour" nor know what does "using UnityEngine;" mean. I closed out of vim (leaving the server open) and reopened the file again, this time I kept getting errors almost each time I try to do anything. I closed out of vim and killed the server, opened up the script again with a new server, and it was able to detect things now. I managed to get completion for Unity objects like Vector2, etc.

@HiPhish
Copy link
Author

HiPhish commented Aug 24, 2014

Typing Vec and pressing ctrl+o is the same. And how do I kill the server?

@vexe
Copy link

vexe commented Aug 24, 2014

On windows just do it from the task manager - go to the processes tab, find the server process and end it.

in mac, sudo kill PID

where PID is the process ID of the server process. you could use pgrep process_name to get the id of a process

@HiPhish
Copy link
Author

HiPhish commented Aug 24, 2014

I used pgrep monoto list all mono processes and killed them, same thing. It might be worth adding that I also get nonsensical completion suggestions, like vertex which I know for a fact is only used as a temporary variable in some functions, it is never a member, yet I get it as a suggestion when I'm at the top of my class (i.e. right after the opening curly braces of my class definition).

@nosami
Copy link
Contributor

nosami commented Aug 24, 2014

@HiPhish you are getting those completions because you are using YouCompleteMe.

@nosami
Copy link
Contributor

nosami commented Aug 24, 2014

btw, there is an :OmniSharpStopServer command.... also pkill mono is enough to kill all mono processes on linux or OSX.

@nosami
Copy link
Contributor

nosami commented Aug 24, 2014

Disable YouCompleteMe, add

autocmd FileType cs setlocal omnifunc=OmniSharp#Complete

if you don't have that line already.... then use <C-x><C-o> (that is ctrl x followed by ctrl o) and see if you still have problems.

@HiPhish
Copy link
Author

HiPhish commented Aug 24, 2014

I commented the YCM plugin in my vimrc, already had that line, killed Mono. My suggestions are still the samem they just don't pop up on their own anymore.

@HiPhish HiPhish closed this as completed Aug 24, 2014
@HiPhish HiPhish reopened this Aug 24, 2014
@nosami
Copy link
Contributor

nosami commented Aug 24, 2014

You still get vertex at the top of the class? I don't believe that. That is impossible if you are using <C-x><C-o> and not just <C-o>, <C-n> or some other keyboard command.

I also have never seen unicode errors... pretty sure they are coming from YCM

@vexe
Copy link

vexe commented Aug 24, 2014

Is the rest of the features working? Find usage, renaming, etc

@HiPhish
Copy link
Author

HiPhish commented Aug 24, 2014

OK, you may officially call me an idiot. I thought I was supposed to press either <C-x> or <C-o>, not <C-x> and <C-o>. I'll see about the other features later, I need to get some fresh air before I do something stupid again. Really sorry for this.

@vexe
Copy link

vexe commented Aug 24, 2014

Welcome to the club. You might find it easier if you mapped it, put this in your vimrc

inoremap <C-o> <C-x><C-o>
inoremap <C-Space> <C-p>

If you want to auto-complete by pressing tabs, you could configure supertab to do it for you

@nosami
Copy link
Contributor

nosami commented Aug 24, 2014

Don't worry about it. Vim has a steep learning curve.

Simplest way to get started is to use SuperTab and put this in your vimrc (it's commented out in my README). Then you can use tab to trigger autocomplete and also to select.

"Super tab settings
let g:SuperTabDefaultCompletionType = 'context'
let g:SuperTabContextDefaultCompletionType = "<c-x><c-o>"
let g:SuperTabDefaultCompletionTypeDiscovery = ["&omnifunc:<c-x><c-o>","&completefunc:<c-x><c-n>"]
let g:SuperTabClosePreviewOnPopupClose = 1

@vexe
Copy link

vexe commented Aug 24, 2014

One thing to watch out for (I just came across it) is that if you're using snipmate or ultisnips you might have issues with supertab, see this ervandew/supertab#74 - in this case, just map the snippet's to or something.

@HiPhish
Copy link
Author

HiPhish commented Aug 24, 2014

Something weird: Let's say I type Vector2 myVec, everything fine. Now I type Vector2 and my finger slips to the ESC button. Again, nothing happens. However, if I type v2, let it autocomplete to Vector2and then my finger slips I get this error at the bottom:

[Scratch] [Preview]
Error detected while processing function <SNR>25_UpdateErrors..71..27..57..<SNR>20__setScreenColumn:
line    3:
E716: Key not present in Dictionary: vcol
Press ENTER or type command to continue

Pressing anything other than ESC yields even more such junk. The problem is, even if I go back and fix the line that error keeps coming back every time I exit insert mode. Is this from OmniSharp or another plugin?

EDIT: when I press gd to go to a definition that's int he same file:

Error detected while processing function OmniSharp#GotoDefinition:
line    1:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/hiphish/.vim/bundle/Omnisharp/python/OmniSharp.py", line 71, in gotoDefinition
    openFile(definition['FileName'].replace("'","''"), definition['Line'], definition['Column'])
  File "/Users/hiphish/.vim/bundle/Omnisharp/python/OmniSharp.py", line 76, in openFile
    vim.command("call OmniSharp#JumpToLocation('%(filename)s', %(line)s, %(column)s)" % locals())
vim.error: Vim(if):E716: Key not present in Dictionary: vcol

@nosami
Copy link
Contributor

nosami commented Aug 24, 2014

I think your issue is the same as this one #151

The second issue is the same I think. Go to definition is triggering the BufEnter event which in turn triggers a Syntastic check.

@HiPhish
Copy link
Author

HiPhish commented Aug 24, 2014

You're right, running git checkout 5c48b30 solved both problems.

@nosami
Copy link
Contributor

nosami commented Aug 24, 2014

It seems that setting let g:syntastic_cursor_column = 0 stops the error with syntastic HEAD

@HiPhish
Copy link
Author

HiPhish commented Aug 25, 2014

Yes, I could revert back to HEAD now.

I'm now getting a number of warnings in my scripts, green arrows pointing to specific code lines. The warnings are:

  • I am supposed to start properties with a capital letter
  • I am supposed to use the var keyword: var newPosition = new Vector3() instead of Vector3 newPosition = new Vector3()
  • It wants me to change if (!relativeSize) into a |= expression and I have no idea what that means.

The first warning is a legitimate one, however, that's the way Unity's API works and in this particular case I don't want to deviate. I'm a plugin developer, not a game developer, so my code will be used by other people who will expect it to work like Unity's API. Point 2 is something I don't really like using the var keyword just because it can be used. Point 3 I don't understand; first I thought it wanted me to type if (relativeSize != true) but then I get a warning about a redundant check (obviously).

Is there a way to turn off those particular kinds of warnings? I do like how it found an unneeded using directive.

@nosami
Copy link
Contributor

nosami commented Aug 25, 2014

They are only suggestions. OmniSharp can attempt to fix these for you with the :OmniSharpFixIssue command (default <leader>fx with my config)

You can exclude warnings here on the server config https://github.com/nosami/OmniSharpServer/blob/master/OmniSharp/config.json#L6

Unfortunately, this only accepts exact string matches and not regex matches or similar. I plan to change that.

You need to rebuild the server to copy the config to the /bin/Debug folder and restart.

@HiPhish
Copy link
Author

HiPhish commented Aug 25, 2014

OK, I removed the second warning. The first one uses the name of the property in the string, so I'll live with it for now. I still don't understand what the third warning means. Any ideas?

@nosami
Copy link
Contributor

nosami commented Aug 25, 2014

@HiPhish I made a change to the server to allow you to use a regex pattern to ignore code issues. See OmniSharp/omnisharp-server#100 and OmniSharp/omnisharp-server@3b56e31

@HiPhish
Copy link
Author

HiPhish commented Aug 26, 2014

Thanks, you are awesome. One little problem though: if I modify config.json I can no longer run Vundle's :PluginUpdate command, because my change will clash with your pristine file. Would it be possible to place such modifications into an external file that the user adds to?

Otherwise I have to comment away OmniSharp in my .vimrc, run :PluginClean, uncomment OmniSharp, run :PluginUpdate and then rebuild my server. Not the worst thing in the world, but... you know ;) If it's too much work don't bother though, but a user manual would still be a good idea.

EDIT: BTW, this issue can be closed as far as I'm concerned, OmniSharp does work with Unity and anything else is outside the scope of this topic.

@HiPhish HiPhish closed this as completed Aug 26, 2014
@nosami
Copy link
Contributor

nosami commented Aug 26, 2014

That's odd.... NeoBundle allows local modifications to files. Doesn't Vundle do git pull --rebase ?

Anyway..... you can specify an external config file by starting the server manually with the switch -config /path/to/config ... but that means you can't use the auto start mechanism currently. You would have to start it manually before loading Vim.

I'm planning on having OmniSharp look for config files within the project source tree to allow for different configs per solution. Would that solve your problem?

@HiPhish
Copy link
Author

HiPhish commented Aug 26, 2014

Yeah, that would be OK, but I wouldn't call it a "problem", rather an "inconvenience". It would be a good idea though, suppressing warnings about lower-case properties makes sense in a Unity project, but could be considered harmful in a regular .NET or Mono project.

I don't know how Vundle works, I only knew about Pathogen, then learned about Vundle and found that one to be nicer. I'll have a look at NeoBundle, but I don't want to change my setup for now after I go things working.

@nosami
Copy link
Contributor

nosami commented Feb 24, 2015

What happens if you run '/home/cc/.vim/bundle/omnisharp-vim/server/OmniSharp/bin/Debug/OmniSharp.exe' -p 2000 -s '/home/cc/MySolution/MySolution.sln' in the terminal?

@Ghopper21
Copy link

Hi @nosami - thanks for all the work you do on OmniSharp. I know this is closed, but it seems like a reasonable place to ask about problems with Unity, since you are still active on it. Let me know if you prefer me to open a new issue.

I've got omnisharp-vim installed with SuperTab and Syntastic, no YCM. Completions, go-to-definition, and find-usages (and syntax checking) are working for some basic examples.

However, here is an example of something that isn't working:

  1. Enter a line "yield return new WaitFor" then hit tab, get "Omni completion (^O^N^P) Pattern not found" error
  2. However, if I enter "WaitFor" on an empty line and hit tab, then completion works, with the three options of WaitForEndOfFrame, WaitForFixedUpdate, and WaitForSeconds correctly showing up.
  3. Likewise, "yield ret" then tab gives no completion for return, while "ret" on its own line followed by tab does.

Let me know if you'd like me try anything on this or anything else Unity-related. I'm pretty motivated to get this working for Unity, so willing to help however I can. (Right now I'm using Xamarin Studio, which has excellent completion/go-to-def/find-usages but very bad vi modes.)

@Ghopper21
Copy link

Btw, regarding @HiPhish's question above about which .sln file to pick, it seems the one to pick is sans -csharp, based on the fact that when you select "Sync MonoDevelop Project" in Unity with Xamarin Studio set as the external editor, Xamarin Studio will load the one without the -csharp name. Having said that, I've tried both and it doesn't seem to make any difference. For example, I get the same behavior noted above with either.

@Ghopper21
Copy link

The plot thickens regarding the WaitFor completion.

If I have the following code:

#if UNITY_EDITOR
yield return new WatiFor

then hit tab on the WaitFor -- it works!

I.e. without the preceding preprocessor directive, it doesn't work, but with it, it does.

In case it helps/matters, here's the Unity doc on WaitForSeconds. It's defined in the UnityEngine namespace, with using UnityEngine being in all standard Unity scripts.

@Ghopper21
Copy link

Another thing: completion doesn't work on UNITY_EDITOR and other preprocessor directives that Unity recognizes. I'm not sure if this is a Unity-specific thing or just that OmniSharp doesn't handle those. For reference, Xamarin Studio correctly autocompletes those.

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

No branches or pull requests

4 participants