CSharpParsedFile Parse(string program) { CompilationUnit cu = new CSharpParser().Parse(new StringReader(program), "test.cs"); CSharpParsedFile parsedFile = cu.ToTypeSystem(); project = project.UpdateProjectContent(null, parsedFile); compilation = project.CreateCompilation(); return parsedFile; }
protected ResolveResult ResolveAtLocation(string code) { CompilationUnit cu = new CSharpParser().Parse(new StringReader(code.Replace("$", "")), "test.cs"); TextLocation[] dollars = FindDollarSigns(code).ToArray(); Assert.AreEqual(1, dollars.Length, "Expected 1 dollar signs marking the location"); SetUp(); CSharpParsedFile parsedFile = cu.ToTypeSystem(); project = project.UpdateProjectContent(null, parsedFile); compilation = project.CreateCompilation(); ResolveResult rr = Resolver.ResolveAtLocation.Resolve(compilation, parsedFile, cu, dollars[0]); return(rr); }
public void ProcessInput(string input, string sourceFile) { if (string.IsNullOrEmpty(sourceFile)) { return; } //see if it contains the word class, enum or struct //todo: this is buggy because if two classes are evaluated seperately, the original file will overwrite it // if the file is a script we should try to extract the class name and use it as the file name. sciptname + class // we can probably use the AST for that. if (input.Contains("class ") || input.Contains("enum ") || input.Contains("struct ")) { var syntaxTree = new CSharpParser().Parse(input, sourceFile); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); projectContent = projectContent.AddOrUpdateFiles(unresolvedFile); } }
protected Tuple <CSharpAstResolver, AstNode> PrepareResolver(string code) { SyntaxTree syntaxTree = new CSharpParser().Parse(code.Replace("$", ""), "code.cs"); TextLocation[] dollars = FindDollarSigns(code).ToArray(); Assert.AreEqual(2, dollars.Length, "Expected 2 dollar signs marking start+end of desired node"); SetUp(); CSharpUnresolvedFile unresolvedFile = syntaxTree.ToTypeSystem(); project = project.AddOrUpdateFiles(unresolvedFile); compilation = project.CreateCompilation(); CSharpAstResolver resolver = new CSharpAstResolver(compilation, syntaxTree, unresolvedFile); return(Tuple.Create(resolver, FindNode(syntaxTree, dollars[0], dollars[1]))); }
protected Tuple <CSharpAstResolver, AstNode> PrepareResolver(string code) { CompilationUnit cu = new CSharpParser().Parse(new StringReader(code.Replace("$", "")), "code.cs"); TextLocation[] dollars = FindDollarSigns(code).ToArray(); Assert.AreEqual(2, dollars.Length, "Expected 2 dollar signs marking start+end of desired node"); SetUp(); CSharpParsedFile parsedFile = cu.ToTypeSystem(); project = project.UpdateProjectContent(null, parsedFile); compilation = project.CreateCompilation(); CSharpAstResolver resolver = new CSharpAstResolver(compilation, cu, parsedFile); return(Tuple.Create(resolver, FindNode(cu, dollars[0], dollars[1]))); }
ResolveResult ResolveFromPosition(string editorText, int offset, string fileName) { if (Project == null) { return(null); } var location = new ReadOnlyDocument(editorText).GetLocation(offset); var syntaxTree = new CSharpParser().Parse(editorText, fileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); Project = Project.AddOrUpdateFiles(unresolvedFile); var compilation = Project.CreateCompilation(); return(ResolveAtLocation.Resolve(compilation, unresolvedFile, syntaxTree, location)); }
private void AddOrUpdateFiles() { var unresolvedFiles = new IUnresolvedFile[this.sourceFiles.Count]; Parallel.For(0, unresolvedFiles.Length, i => { var file = this.sourceFiles[i]; var syntaxTree = new CSharpParser().Parse(System.IO.File.ReadAllText(file), file); if (this.CanFreeze) { //syntaxTree.Freeze(); } unresolvedFiles[i] = syntaxTree.ToTypeSystem(); }); this.project = this.project.AddOrUpdateFiles(unresolvedFiles); this.compilation = this.project.CreateCompilation(); }
private IProjectContent AddFileToProject(IProjectContent project, string fileName) { string code = string.Empty; try { code = System.IO.File.ReadAllText(fileName); } catch (Exception) { _log.ErrorFormat("Could not find file to AddFileToProject, Name: {0}", fileName); } SyntaxTree syntaxTree = new CSharpParser().Parse(code, fileName); CSharpUnresolvedFile unresolvedFile = syntaxTree.ToTypeSystem(); if (syntaxTree.Errors.Count == 0) { project = project.AddOrUpdateFiles(unresolvedFile); } return(project); }
protected Tuple <CSharpAstResolver, AstNode> PrepareResolver(string code) { CompilationUnit cu = new CSharpParser().Parse(new StringReader(code.Replace("$", "")), "code.cs"); TextLocation[] dollars = FindDollarSigns(code).ToArray(); Assert.AreEqual(2, dollars.Length, "Expected 2 dollar signs marking start+end of desired node"); SetUp(); CSharpParsedFile parsedFile = cu.ToTypeSystem(); project = project.UpdateProjectContent(null, parsedFile); compilation = project.CreateCompilation(); FindNodeVisitor fnv = new FindNodeVisitor(dollars[0], dollars[1]); cu.AcceptVisitor(fnv, null); Assert.IsNotNull(fnv.ResultNode, "Did not find DOM node at the specified location"); CSharpAstResolver resolver = new CSharpAstResolver(compilation, cu, parsedFile); return(Tuple.Create(resolver, fnv.ResultNode)); }
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 are there 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 TestCompletionDataFactory(resolver3), pctx, resolver3.CurrentTypeResolveContext); engine.EolMarker = Environment.NewLine; engine.FormattingPolicy = FormattingOptionsFactory.CreateMono(); var data = engine.GetCompletionData(offset, controlSpace: false); return(data); }
/// <summary> /// Initializes a new instance of the <see cref="CSharpCompletionContext"/> class. /// </summary> /// <param name="document">The document, make sure the FileName property is set on the document.</param> /// <param name="offset">The offset.</param> /// <param name="projectContent">Content of the project.</param> /// <param name="usings">The usings.</param> public CSharpCompletionContext(IDocument document, int offset, IProjectContent projectContent, string usings = null) { OriginalDocument = document; OriginalOffset = offset; //if the document is a c# script we have to soround the document with some code. Document = PrepareCompletionDocument(document, ref offset, usings); Offset = offset; var syntaxTree = new CSharpParser().Parse(Document, Document.FileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); ProjectContent = projectContent.AddOrUpdateFiles(unresolvedFile); //note: it's important that the project content is used that is returned after adding the unresolved file Compilation = ProjectContent.CreateCompilation(); var location = Document.GetLocation(Offset); Resolver = unresolvedFile.GetResolver(Compilation, location); TypeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(Compilation, location); CompletionContextProvider = new DefaultCompletionContextProvider(Document, unresolvedFile); }
public void SetUp() { SD.InitializeForUnitTests(); textEditor = new MockTextEditor(); textEditor.Document.Text = programStart + "override " + programEnd; textEditor.Caret.Offset = programStart.Length + "override ".Length; var parseInfo = textEditor.CreateParseInformation(); var pc = new CSharpProjectContent().AddOrUpdateFiles(parseInfo.UnresolvedFile); pc = pc.AddAssemblyReferences(new[] { Corlib }); var compilation = pc.CreateCompilation(); SD.Services.AddService(typeof(IParserService), MockRepository.GenerateStrictMock <IParserService>()); SD.ParserService.Stub(p => p.GetCachedParseInformation(textEditor.FileName)).Return(parseInfo); SD.ParserService.Stub(p => p.GetCompilationForFile(textEditor.FileName)).Return(compilation); SD.ParserService.Stub(p => p.Parse(textEditor.FileName, textEditor.Document)).WhenCalled( i => { var syntaxTree = new CSharpParser().Parse(textEditor.Document, textEditor.FileName); i.ReturnValue = new CSharpFullParseInformation(syntaxTree.ToTypeSystem(), null, syntaxTree); }); CSharpCompletionBinding completion = new CSharpCompletionBinding(); keyPressResult = completion.HandleKeyPressed(textEditor, ' '); }
// i'm not using this... public static CSharpCompletionEngine CreateEngine(string text, out int cursorPosition, params IUnresolvedAssembly[] references) { string parsedText; string editorText; cursorPosition = text.IndexOf('$'); int endPos = text.IndexOf('$', cursorPosition + 1); if (endPos == -1) { if (cursorPosition < 0) { parsedText = editorText = text; } else { parsedText = editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1); } } else { parsedText = text.Substring(0, cursorPosition) + new string(' ', endPos - cursorPosition) + text.Substring(endPos + 1); editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring(endPos + 1); cursorPosition = endPos - 1; } var doc = new ReadOnlyDocument(editorText); IProjectContent projContent = new CSharpProjectContent(); var refs = new List <IUnresolvedAssembly> { mscorlib.Value, systemCore.Value, systemAssembly.Value, systemXmlLinq.Value }; if (references != null) { refs.AddRange(references); } projContent = projContent.AddAssemblyReferences(refs); // Parse => SyntaxTree var syntaxTree = new CSharpParser().Parse(parsedText, "program.cs"); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); // Add CSharpUnresolvedFile to CSharpProjectContent projContent = projContent.AddOrUpdateFiles(unresolvedFile); // Create a TypeSystem.ICompilation that allows resolving within the project. var compilation = projContent.CreateCompilation(); var textCursorLocation = cursorPosition > 0 ? doc.GetLocation(cursorPosition) : new TextLocation(1, 1); #region Create and Refine the type resolution context as much as possible var typeResolveContext = new CSharpTypeResolveContext(compilation.MainAssembly); typeResolveContext = typeResolveContext.WithUsingScope(unresolvedFile.GetUsingScope(textCursorLocation).Resolve(compilation)); var curDef = unresolvedFile.GetInnermostTypeDefinition(textCursorLocation); if (curDef != null) { var resolvedDef = curDef.Resolve(typeResolveContext).GetDefinition(); typeResolveContext = typeResolveContext.WithCurrentTypeDefinition(resolvedDef); var curMember = resolvedDef.Members.FirstOrDefault(m => m.Region.Begin <= textCursorLocation && textCursorLocation < m.BodyRegion.End); if (curMember != null) { typeResolveContext = typeResolveContext.WithCurrentMember(curMember); } } #endregion // Cool! Marry the concept of content & typed var completionContext = new DefaultCompletionContextProvider(doc, unresolvedFile); #region Add Preprocessor Symbols completionContext.AddSymbol("TEST"); foreach (var sym in syntaxTree.ConditionalSymbols) { completionContext.AddSymbol(sym); } #endregion var engine = new CSharpCompletionEngine(doc, completionContext, new TestCompletionDataFactory(new CSharpResolver(typeResolveContext)), projContent, typeResolveContext); engine.EolMarker = Environment.NewLine; engine.FormattingPolicy = FormattingOptionsFactory.CreateMono(); return(engine); }
public static void CreateCompilation (string parsedText, out IProjectContent pctx, out SyntaxTree syntaxTree, out CSharpUnresolvedFile unresolvedFile, bool expectErrors, params IUnresolvedAssembly[] references) { pctx = new CSharpProjectContent(); var refs = new List<IUnresolvedAssembly> { mscorlib.Value, systemCore.Value, systemAssembly.Value, systemXmlLinq.Value }; if (references != null) refs.AddRange (references); pctx = pctx.AddAssemblyReferences(refs); syntaxTree = new CSharpParser().Parse(parsedText, "program.cs"); syntaxTree.Freeze(); if (!expectErrors && syntaxTree.Errors.Count > 0) { Console.WriteLine ("----"); Console.WriteLine (parsedText); Console.WriteLine ("----"); foreach (var error in syntaxTree.Errors) Console.WriteLine (error.Message); Assert.Fail ("Parse error."); } unresolvedFile = syntaxTree.ToTypeSystem(); pctx = pctx.AddOrUpdateFiles(unresolvedFile); }
internal static IParameterDataProvider CreateProvider (string text) { string parsedText; string editorText; int cursorPosition = text.IndexOf ('$'); int endPos = text.IndexOf ('$', cursorPosition + 1); if (endPos == -1) parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1); else { parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1); editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1); cursorPosition = endPos - 1; } var doc = new ReadOnlyDocument (editorText); IProjectContent pctx = new CSharpProjectContent (); pctx = pctx.AddAssemblyReferences (new [] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore }); var compilationUnit = new CSharpParser ().Parse (parsedText, "program.cs"); var parsedFile = compilationUnit.ToTypeSystem (); pctx = pctx.UpdateProjectContent (null, parsedFile); var cmp = pctx.CreateCompilation (); var loc = doc.GetLocation (cursorPosition); var engine = new CSharpParameterCompletionEngine (doc, new TestFactory (pctx)); var rctx = new CSharpTypeResolveContext (cmp.MainAssembly); rctx = rctx.WithUsingScope (parsedFile.GetUsingScope (loc).Resolve (cmp)); var curDef = parsedFile.GetInnermostTypeDefinition (loc); if (curDef != null) { rctx = rctx.WithCurrentTypeDefinition (curDef.Resolve (rctx).GetDefinition ()); var curMember = parsedFile.GetMember (loc); if (curMember != null) rctx = rctx.WithCurrentMember (curMember.CreateResolved (rctx)); } engine.ctx = rctx; engine.CSharpParsedFile = parsedFile; engine.ProjectContent = pctx; engine.Unit = compilationUnit; return engine.GetParameterDataProvider (cursorPosition, doc.GetCharAt (cursorPosition - 1)); }
public override IEnumerable <MemberReference> FindReferences(Project project, IProjectContent content, IEnumerable <FilePath> possibleFiles, IEnumerable <object> members) { if (project == null) { throw new ArgumentNullException("project", "Project not set."); } if (content == null) { throw new ArgumentNullException("content", "Project content not set."); } SetPossibleFiles(possibleFiles); SetSearchedMembers(members); var scopes = searchedMembers.Select(e => refFinder.GetSearchScopes(e as IEntity)); var compilation = TypeSystemService.GetCompilation(project); List <MemberReference> refs = new List <MemberReference> (); foreach (var opendoc in openDocuments) { foreach (var newRef in FindInDocument(opendoc.Item2)) { if (newRef == null || refs.Any(r => r.FileName == newRef.FileName && r.Region == newRef.Region)) { continue; } refs.Add(newRef); } } foreach (var file in files) { string text = Mono.TextEditor.Utils.TextFileUtility.ReadAllText(file); if (memberName != null && text.IndexOf(memberName, StringComparison.Ordinal) < 0 && (keywordName == null || text.IndexOf(keywordName, StringComparison.Ordinal) < 0)) { continue; } using (var editor = TextEditorData.CreateImmutable(text)) { editor.Document.FileName = file; var unit = new CSharpParser().Parse(editor); if (unit == null) { continue; } var storedFile = content.GetFile(file); var parsedFile = storedFile as CSharpParsedFile; if (parsedFile == null && storedFile is ParsedDocumentDecorator) { parsedFile = ((ParsedDocumentDecorator)storedFile).ParsedFile as CSharpParsedFile; } if (parsedFile == null) { // for fallback purposes - should never happen. parsedFile = unit.ToTypeSystem(); content = content.UpdateProjectContent(content.GetFile(file), parsedFile); compilation = content.CreateCompilation(); } foreach (var scope in scopes) { refFinder.FindReferencesInFile( scope, parsedFile, unit, compilation, (astNode, result) => { var newRef = GetReference(result, astNode, file, editor); if (newRef == null || refs.Any(r => r.FileName == newRef.FileName && r.Region == newRef.Region)) { return; } refs.Add(newRef); }, CancellationToken.None ); } } } return(refs); }
static CompletionDataList CreateProvider (string text, bool isCtrlSpace) { string parsedText; string editorText; int cursorPosition = text.IndexOf ('$'); int endPos = text.IndexOf ('$', cursorPosition + 1); if (endPos == -1) { parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1); } else { parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1); editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1); cursorPosition = endPos - 1; } var doc = new ReadOnlyDocument (editorText); IProjectContent pctx = new CSharpProjectContent (); pctx = pctx.AddAssemblyReferences (new [] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore }); var compilationUnit = new CSharpParser ().Parse (parsedText, "program.cs"); compilationUnit.Freeze (); var parsedFile = compilationUnit.ToTypeSystem (); pctx = pctx.UpdateProjectContent (null, parsedFile); var cmp = pctx.CreateCompilation (); var loc = doc.GetLocation (cursorPosition); var rctx = new CSharpTypeResolveContext (cmp.MainAssembly); rctx = rctx.WithUsingScope (parsedFile.GetUsingScope (loc).Resolve (cmp)); var curDef = parsedFile.GetInnermostTypeDefinition (loc); if (curDef != null) { var resolvedDef = curDef.Resolve (rctx).GetDefinition (); rctx = rctx.WithCurrentTypeDefinition (resolvedDef); var curMember = resolvedDef.Members.FirstOrDefault (m => m.Region.Begin <= loc && loc < m.BodyRegion.End); if (curMember != null) rctx = rctx.WithCurrentMember (curMember); } var engine = new CSharpCompletionEngine (doc, new TestFactory (), pctx, rctx, compilationUnit, parsedFile); engine.EolMarker = Environment.NewLine; engine.FormattingPolicy = FormattingOptionsFactory.CreateMono (); var data = engine.GetCompletionData (cursorPosition, isCtrlSpace); return new CompletionDataList () { Data = data, AutoCompleteEmptyMatch = engine.AutoCompleteEmptyMatch, AutoSelect = engine.AutoSelect, DefaultCompletionString = engine.DefaultCompletionString }; }
public static CSharpCompletionEngine CreateEngine(string text, out int cursorPosition, params IUnresolvedAssembly[] references) { string parsedText; string editorText; cursorPosition = text.IndexOf('$'); int endPos = text.IndexOf('$', cursorPosition + 1); if (endPos == -1) { if (cursorPosition < 0) { parsedText = editorText = text; } else { parsedText = editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1); } } else { parsedText = text.Substring(0, cursorPosition) + new string(' ', endPos - cursorPosition) + text.Substring(endPos + 1); editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring(endPos + 1); cursorPosition = endPos - 1; } var doc = new ReadOnlyDocument(editorText); IProjectContent pctx = new CSharpProjectContent(); var refs = new List<IUnresolvedAssembly> { mscorlib.Value, systemCore.Value, systemAssembly.Value, systemXmlLinq.Value }; if (references != null) refs.AddRange (references); pctx = pctx.AddAssemblyReferences(refs); var syntaxTree = new CSharpParser().Parse(parsedText, "program.cs"); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); pctx = pctx.AddOrUpdateFiles(unresolvedFile); var cmp = pctx.CreateCompilation(); var loc = cursorPosition > 0 ? doc.GetLocation(cursorPosition) : new TextLocation (1, 1); var rctx = new CSharpTypeResolveContext(cmp.MainAssembly); rctx = rctx.WithUsingScope(unresolvedFile.GetUsingScope(loc).Resolve(cmp)); var curDef = unresolvedFile.GetInnermostTypeDefinition(loc); if (curDef != null) { var resolvedDef = curDef.Resolve(rctx).GetDefinition(); rctx = rctx.WithCurrentTypeDefinition(resolvedDef); var curMember = resolvedDef.Members.FirstOrDefault(m => m.Region.Begin <= loc && loc < m.BodyRegion.End); if (curMember != null) { rctx = rctx.WithCurrentMember(curMember); } } var mb = new DefaultCompletionContextProvider(doc, unresolvedFile); mb.AddSymbol ("TEST"); var engine = new CSharpCompletionEngine(doc, mb, new TestFactory(new CSharpResolver (rctx)), pctx, rctx); engine.EolMarker = Environment.NewLine; engine.FormattingPolicy = FormattingOptionsFactory.CreateMono(); return engine; }