Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8633853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:34:13+00:00 2026-06-12T09:34:13+00:00

I just found out about NRefactory 5 and I would guess, that it is

  • 0

I just found out about NRefactory 5 and I would guess, that it is the most suitable solution for my current problem. At the moment I’m developing a little C# scripting application for which I would like to provide code completion. Until recently I’ve done this using the “Roslyn” project from Microsoft. But as the latest update of this project requires .Net Framework 4.5 I can’t use this any more as I would like the app to run under Win XP as well. So I have to switch to another technology here.

My problem is not the compilation stuff. This can be done, with some more effort, by .Net CodeDomProvider as well. The problem ist the code completion stuff. As far as I know, NRefactory 5 provides everything that is required to provide code completion (parser, type system etc.) but I just can’t figure out how to use it. I took a look at SharpDevelop source code but they don’t use NRefactory 5 for code completion there, they only use it as decompiler. As I couldn’t find an example on how to use it for code completion in the net as well I thought that I might find some help here.

The situation is as follows. I have one single file containing the script code. Actually it is not even a file but a string which I get from the editor control (by the way: I’m using AvalonEdit for this. Great editor!) and some assemblies that needs to get referenced. So, no solution files, no project files etc. just one string of source code and the assemblies.

I’ve taken a look at the Demo that comes with NRefactory 5 and the article on code project and got up with something like this:

var unresolvedTypeSystem = syntaxTree.ToTypeSystem();

IProjectContent pc = new CSharpProjectContent();

// Add parsed files to the type system
pc = pc.AddOrUpdateFiles(unresolvedTypeSystem);

// Add referenced assemblies:
pc = pc.AddAssemblyReferences(new CecilLoader().LoadAssemblyFile(
    System.Reflection.Assembly.GetAssembly(typeof(Object)).Location));

My problem is that I have no clue on how to go on. I’m not even sure if it is the right approach to accomplish my goal. How to use the CSharpCompletionEngine? What else is required? etc. You see there are many things that are very unclear at the moment and I hope you can bring some light into this.

Thank you all very much in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:34:15+00:00Added an answer on June 12, 2026 at 9:34 am

    Take a look at method ICSharpCode.NRefactory.CSharp.CodeCompletion.CreateEngine. You need to create an instance of CSharpCompletionEngine and pass in the correct document and the resolvers. I managed to get it working for CTRL+Space compltition scenario. However I am having troubles with references to types that are in other namespaces. It looks like CSharpTypeResolveContext does not take into account the using namespace statements – If I resolve the references with CSharpAstResolver, they are resolved OK, but I am unable to correctly use this resolver in code completition scenario…

    UPDATE #1:

    I’ve just managed to get the working by obtaining resolver from unresolved fail.

    Here is the snippet:

    var mb = new DefaultCompletionContextProvider(doc, unresolvedFile);
    var resolver3 = unresolvedFile.GetResolver(cmp, loc); // get the resolver from unresolvedFile
    var engine = new CSharpCompletionEngine(doc, mb, new CodeCompletionBugTests.TestFactory(resolver3), pctx, resolver3.CurrentTypeResolveContext );
    

    Update #2:

    Here is the complete method. It references classes from unit test projects, sou you would need to reference/copy them into your project:

    public static IEnumerable<ICompletionData> DoCodeComplete(string editorText, int offset) // not the best way to put in the whole string every time
    {
    
      var doc = new ReadOnlyDocument(editorText);
      var location = doc.GetLocation(offset);
    
      string parsedText = editorText; // TODO: Why there are different values in test cases?
    
    
      var syntaxTree = new CSharpParser().Parse(parsedText, "program.cs");
      syntaxTree.Freeze();
      var unresolvedFile = syntaxTree.ToTypeSystem();
    
      var mb = new DefaultCompletionContextProvider(doc, unresolvedFile);
    
      IProjectContent pctx = new CSharpProjectContent();
      var refs = new List<IUnresolvedAssembly> { mscorlib.Value, systemCore.Value, systemAssembly.Value}; 
      pctx = pctx.AddAssemblyReferences(refs);
      pctx = pctx.AddOrUpdateFiles(unresolvedFile);
    
      var cmp = pctx.CreateCompilation();
    
      var resolver3 = unresolvedFile.GetResolver(cmp, location);
      var engine = new CSharpCompletionEngine(doc, mb, new CodeCompletionBugTests.TestFactory(resolver3), pctx, resolver3.CurrentTypeResolveContext );
    
    
      engine.EolMarker = Environment.NewLine;
      engine.FormattingPolicy = FormattingOptionsFactory.CreateMono();
    
      var data = engine.GetCompletionData(offset, controlSpace: false);
      return data;
    
    }
    

    }

    Hope it helps,
    Matra

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scheme relative URLs (network-path references) are something that I've just found out about -
I just found out about BuddyPress (a collection of plugins that convert a WordPress
I just found out, that one of my test files is missing about 20
I just found out about ie7-js ; IE7 is a JavaScript library to make
I just found out about superfish and currently using it on an asp.net. The
I just found out about Alice and Scratch. I will be implementing those pretty
I just found out about yield return , I seems really nice. I use
I've just found out about View States in Flex (v3.0), but I am not
Just found out that the video output of the iPad is not a system
I just found out that every time onclick event for my <button> placed inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.