Add support for parsing C# scripts #194

Open
opened 2013-06-21 11:02:22 -05:00 by lukebuehler · 3 comments
lukebuehler commented 2013-06-21 11:02:22 -05:00 (Migrated from github.com)

When using NRefactory for code completion with C# scripts (.csx) I have to surround the script code with a class and method declaration. I could really need this for my C# scripting project: https://github.com/ArnovaAssetManagement/CShell

A script like this:

var x = 10;
Console.WriteLine(x*x);

has to be surrounded like this and then parsed in NRefactory

class MyScript(){
    public static MyScripMethod(){
        var x = 10;
        Console.WriteLine(x*x);
    }
}

This works okey for now, but I cant really support real Roslyn style scripts like this:

#r "somedll.dll"
static class MyMath
{
    public static long Fibonacci(long n)
    {
        //anything more than 48 will result in a stack overflow.
        if (n >= 48) throw new ArgumentException("Enter a number less than 48");
        if (n == 0) return 0;
        if (n == 1) return 1;
        return Fibonacci(n - 1) + Fibonacci(n - 2);
    }
}
*/
// Now the method can be called like so:
MyMath.Fibonacci(12);
When using NRefactory for code completion with C# scripts (.csx) I have to surround the script code with a class and method declaration. I could really need this for my C# scripting project: https://github.com/ArnovaAssetManagement/CShell A script like this: ``` var x = 10; Console.WriteLine(x*x); ``` has to be surrounded like this and then parsed in NRefactory ``` class MyScript(){ public static MyScripMethod(){ var x = 10; Console.WriteLine(x*x); } } ``` This works okey for now, but I cant really support real Roslyn style scripts like this: ``` #r "somedll.dll" static class MyMath { public static long Fibonacci(long n) { //anything more than 48 will result in a stack overflow. if (n >= 48) throw new ArgumentException("Enter a number less than 48"); if (n == 0) return 0; if (n == 1) return 1; return Fibonacci(n - 1) + Fibonacci(n - 2); } } */ // Now the method can be called like so: MyMath.Fibonacci(12); ```
lukebuehler commented 2014-05-21 09:57:46 -05:00 (Migrated from github.com)

Any feedback on this? If this is not added as a feature, how would you recommend constructing the document that is used finally for code completion?

  • I'd say the imports with #r can be my problem to preprocess them.
  • But what about using statements?
  • What about class, struct, and enum declarations?

How do I get best from a script containing this:

using System.Linq;
public class My{
  public int[] Data
}
var m = new My(){Data=new[]{1,2,3,4,5,6}};
//I want Linq auto completion here:
var m2 = m.Data.Whe

To a document that has been automatically "refactored" to look like this:

using System.Linq;

public class My{
  public int[] Data
}

public static class Script
{
   public static void ScriptCode()
   {
      var m = new My(){Data=new[]{1,2,3,4,5,6}};
      //I want Linq auto completion here:
      var m2 = m.Data.Whe
    }
}

I can take care of course that the right dlls are referenced in the workspace and that the caretOffset is tracked right. But what would be the best strategy to get to such a document for code completion?

Any feedback on this? If this is not added as a feature, how would you recommend constructing the document that is used finally for code completion? - I'd say the imports with `#r` can be my problem to preprocess them. - But what about using statements? - What about class, struct, and enum declarations? How do I get best from a script containing this: ``` using System.Linq; public class My{ public int[] Data } var m = new My(){Data=new[]{1,2,3,4,5,6}}; //I want Linq auto completion here: var m2 = m.Data.Whe ``` To a document that has been automatically "refactored" to look like this: ``` using System.Linq; public class My{ public int[] Data } public static class Script { public static void ScriptCode() { var m = new My(){Data=new[]{1,2,3,4,5,6}}; //I want Linq auto completion here: var m2 = m.Data.Whe } } ``` I can take care of course that the right dlls are referenced in the workspace and that the caretOffset is tracked right. But what would be the best strategy to get to such a document for code completion?
mkrueger commented 2014-05-21 12:48:55 -05:00 (Migrated from github.com)

I switch NRefactory to roslyn recently - in the roslyn branch:
https://github.com/icsharpcode/NRefactory/tree/roslyn

And the completion APIs should be easier to use as well.

I switch NRefactory to roslyn recently - in the roslyn branch: https://github.com/icsharpcode/NRefactory/tree/roslyn And the completion APIs should be easier to use as well.
lukebuehler commented 2015-08-18 11:24:40 -05:00 (Migrated from github.com)

With the split of the roslyn NRefactory into RefactoringEssentials, where does that leave code completions stuff that is based on Roslyn?

With the split of the roslyn NRefactory into RefactoringEssentials, where does that leave code completions stuff that is based on Roslyn?
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-icsharpcode/NRefactory#194
No description provided.