GithubHelp home page GithubHelp logo

VS Code support about roslynator HOT 37 CLOSED

dotnet avatar dotnet commented on May 19, 2024 11
VS Code support

from roslynator.

Comments (37)

josefpihrt avatar josefpihrt commented on May 19, 2024 14

Roslynator for Visual Studio Code is here! https://marketplace.visualstudio.com/items?itemName=josefpihrt-vscode.roslynator

from roslynator.

VahidN avatar VahidN commented on May 19, 2024 8

A PowerShell script to automate this process

Write-Host "Download, unzip and enable Roslynator for Visual Studio Code"

$name = "josefpihrt.Roslynator2017"
$url = "https://marketplace.visualstudio.com/items?itemName=$name"
$currentDir = $PSScriptRoot
$file = "$currentDir\Roslynator.zip"

$pattern = "<script class=`"vss-extension`" defer=`"defer`" type=`"application\/json`">(.*?)<\/script>"
$regex = [regex]"(?m)$pattern"
Write-Host "Grab the home page of the $name."
$dom = (New-Object Net.WebClient).DownloadString($url); 
if($dom -and $dom -match $pattern) 
{
    $matches = $regex.Match($dom)
    $jsonText = $matches[0].Groups[1]

    $json = ConvertFrom-Json $jsonText

    $version = $Json.versions[0].version # Parse the json in the page for the latest version number
    $parts = $name.Split(".")
    $publisher = $parts[0]
    $package = $parts[1]

    # Assemble the url for the vsix package	
    $packageUrl = "https://marketplace.visualstudio.com/_apis/public/gallery/publishers/$publisher/vsextensions/$package/$version/vspackage"	
    Write-Host "Download the vsix package: $packageUrl"
    (New-Object Net.WebClient).DownloadFile($packageUrl, $file)
	
    Write-Host "Using $currentDir as the current dir."
    Write-Host "Unzip $file."
    $shellApp = new-object -com shell.application 
    $zipFile = $shellApp.namespace($file) 
    $destination = $shellApp.namespace($currentDir) 
    $destination.Copyhere($zipFile.items(), 0x14)	# overwrite and be silent
	
    Write-Host "Delete VS specific files. Otherwise they will interfere with the MEF services inside OmniSharp."
    Remove-Item "$currentDir\Roslynator.VisualStudio.Core.dll","$currentDir\Roslynator.VisualStudio.dll", "$currentDir\Roslynator.VisualStudio.pkgdef"
	
    $omnisharpJsonFilePath = "$env:USERPROFILE\.omnisharp\omnisharp.json";	
    Write-Host "Create $omnisharpJsonFilePath file."
    $omnisharpJson = @" 
{{
  "RoslynExtensionsOptions": {{
    "LocationPaths": [
      "{0}"
    ]
  }}
}}
"@ -f $currentDir -Replace "\\","\\"
    $omnisharpJson | Out-File "$omnisharpJsonFilePath" -Confirm
	
    Write-Host "Done!"
}
else
{
    Write-Host "Failed to find the packageUrl!"
}

from roslynator.

divinebovine avatar divinebovine commented on May 19, 2024 3

Roslyn analyzers and code fixes support was merged recently into omnisharp-roslyn so this should now be possible.

Until a new preference and an updated stable release of omnisharp-roslyn gets shipped, you'll need to use "omnisharp.path": "latest" in your settings.json and the following in your omnisharp.json:

{
    "RoslynExtensionsOptions": {
        "EnableAnalyzersSupport": true,
        "LocationPaths": [
            "C:\\lib\\roslynator"
        ]
    }
}

I've tested this recently and was able to get both analyzer warnings/errors as well as codefixes.

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024 3

I think this should work:

string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

string path = @"%LOCALAPPDATA%/JosefPihrt/Roslynator/VisualStudioCode";

path = path.Replace("%LOCALAPPDATA%", localAppDataPath);

// "/home/foo/.local/share/JosefPihrt/Roslynator/VisualStudioCode" on Linux with user "foo"
Console.WriteLine(path);

On Windows there will be mixed slashes but I have checked that it's fine.

from roslynator.

domints avatar domints commented on May 19, 2024 1

I'll give it a try then, and report the results :)

from roslynator.

divinebovine avatar divinebovine commented on May 19, 2024 1

Can we remove the blocked label at this point?

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024

I would like to, but currently it is not possible.

See dotnet/vscode-csharp#43

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024

https://www.strathweb.com/2017/05/using-roslyn-refactorings-with-omnisharp-and-visual-studio-code/

from roslynator.

snielsson avatar snielsson commented on May 19, 2024

So is this still blocked? Or is it now possible to make a VSCode Roslynator Add-in?

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024

I don't think it is possible yet, you can check that the issue mentioned above is still open.

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024

How to enable Roslynator for VS Code

from roslynator.

colin-young avatar colin-young commented on May 19, 2024

@josefpihrt I've tried following those instructions, but get:

System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default [...]

I fixed that by "unblocking" the assemblies in Windows (7). Now I'm getting:

Expected to use Roslynator.CSharp.CodeFixes, Version=1.6.30.0, Culture=neutral, PublicKeyToken=94dac60bcac1903d in host services but the assembly cannot be loaded due to an exception: Could not load file or assembly 'Roslynator.Workspaces, Version=1.6.30.0, Culture=neutral, PublicKeyToken=8ca34578834dd224' or one of its dependencies. The system cannot find the file specified.. System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

Any suggestions?

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024

Copying Roslynator.Workspaces.dll to your folder should solve the issue.

I have recently move code dependent on Microsoft.CodeAnalysis.CSharp.Workspaces to new assembly Roslynator.Workspaces but I have not update documentation yet.

from roslynator.

colin-young avatar colin-young commented on May 19, 2024

Thanks! One other thing to note is that I needed to restart VS Code to get it to load the DLLs. Simply restarting the Omnisharp extension was not sufficient. Maybe that's only the case when there are problems reading assemblies, and it's not a general problem if you manage to get your configuration right on your first try.

  1. Extract DLLs
  2. Unblock DLLs in Windows
  3. Add/create settings in omnisharp.json
  4. Start (or restart) VS Code

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024

I'm glad it works!

I will update documentation accordingly.

from roslynator.

domints avatar domints commented on May 19, 2024

Are these DLLs .NET Standard? I mean, would that work with VS Code on Mac?

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024

Roslynator targets .NET Standard 1.3

from roslynator.

divinebovine avatar divinebovine commented on May 19, 2024

Looks like the new preference is available in the latest omnisharp. This will remove the need for the "EnableAnalyzersSupport" setting in your omnisharp.json.

image

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024

Does anybody know how to pack roslyn libraries as VS Code extension?

from roslynator.

sbmw avatar sbmw commented on May 19, 2024

This is fantastic news!

One thing. Currently user-level rules can be set by creating files at %LOCALAPPDATA%\JosefPihrt\Roslynator. This isn't possible for people running VS Code on non-Windows environments.

I think you could just update roslyn/common/roslynator.config within the extension to include a couple of extra imports like:

<Include Path="~/roslynator.config" />
...
<RuleSet Path="~/roslynator.ruleset" />

and users would be able to create a config and ruleset in their home directory?

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024

I should not be necessary to add more paths to roslynator config. It should be sufficient to replace %LOCALAPPDATA% with value of returned by Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).

I do not have OSX or Linux installed on my machine but I found this page that lists all values:

ENM: System.Environment.SpecialFolder.LocalApplicationData
WIN: C:\Users\foo\AppData\Local
LIN: /home/foo/.local/share
OSX: /Users/foo/.local/share

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024

Is anybody able to test that following code snippet will work on non-Windows environment?

using System;
using System.IO;
using System.Diagnostics;

string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

string path = @"%LOCALAPPDATA%\JosefPihrt\Roslynator\VisualStudioCode";

path = path.Replace("%LOCALAPPDATA%", localAppDataPath);

// normalize directory separator
path = Path.GetFullPath(path);

// LINUX
Debug.Assert(path == "/home/foo/.local/share/JosefPihrt/Roslynator/VisualStudioCode");

// OSX
Debug.Assert(path == "/Users/foo/.local/share/JosefPihrt/Roslynator/VisualStudioCode");

Thanks.

from roslynator.

domints avatar domints commented on May 19, 2024

When I replaced /home/foo/ with my current user directory on Linux Assert failed because of wrong slashes.
Otherwise path looks fine (I added Console.WriteLine) :)

My Linux is some kind of Debian (9 I think) and my dotnet version is 2.2.300

from roslynator.

sbmw avatar sbmw commented on May 19, 2024

@josefpihrt yes, your way is definitely better than adding more paths.

As domints says, the directory normalizing isn't working.

You could do something like

if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
    path = path.Replace(@"\", "/");
}

Or else I think just using forward slashes as directory separators will work across all platforms?

from roslynator.

divinebovine avatar divinebovine commented on May 19, 2024

You could use Path.PathSeparator for platform aware separators

from roslynator.

domints avatar domints commented on May 19, 2024

@sbmw that's not the good way :) I believe System.IO.Path has field specifying the correct path delimiter. It's DirectorySeparatorChar. But as AltDirectorySeparatorChar specifies on Windows, Windows also understands / slash, so that might be a way to go.

I think GetFullPath didn't fix slashes on Linux, because neither DirectorySeparatorChar nor AltDirectorySeparatorChar specify \ as proper separator on Linux.

@divinebovine Path.PathSeparator is meant to separate multiple paths, not directories in single path. It resolves to ; on Windows and : on Linux.

from roslynator.

ADIX7 avatar ADIX7 commented on May 19, 2024

There is Path.Combine to combine path parts.

from roslynator.

divinebovine avatar divinebovine commented on May 19, 2024

@domints you're right, my mistake

from roslynator.

domints avatar domints commented on May 19, 2024

@ADIX7

There is Path.Combine to combine path parts.

yet it doesn't fix anything :)

from roslynator.

ADIX7 avatar ADIX7 commented on May 19, 2024

Realy, I haven't read back enough posts...
Joining to @domints: doesn't / work on windows? Or you can expect / and replace it with Path.DirectorySeparatorChar.

According to this / should work on windows too:
https://docs.microsoft.com/en-us/dotnet/api/system.io.path.directoryseparatorchar?view=netframework-4.8

from roslynator.

wocar avatar wocar commented on May 19, 2024

how can i get roslynator to work with macos

from roslynator.

domints avatar domints commented on May 19, 2024

how can i get roslynator to work with macos

Just install VS Code extension and modify one file according to readme on extension page

from roslynator.

wocar avatar wocar commented on May 19, 2024

from roslynator.

domints avatar domints commented on May 19, 2024

image
It is there ;)

from roslynator.

wocar avatar wocar commented on May 19, 2024

Ok the assemblies seem to load but still doesnt work :(

Can someone else confirm that works for mac

from roslynator.

domints avatar domints commented on May 19, 2024

@wocar
obraz

from roslynator.

josefpihrt avatar josefpihrt commented on May 19, 2024

I'm going to close this issue. Please file a new issue for anything related to VS Code. Thanks.

from roslynator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.