void PreloadThread() { // Pre-load some stuff to make SharpDevelop more responsive once it is started. LoggingService.Debug("Preload-Thread started."); // warm up MSBuild string projectCode = @" <Project DefaultTargets=""Build"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" ToolsVersion=""4.0""> <PropertyGroup> <Configuration>Debug</Configuration> <Platform>AnyCPU</Platform> </PropertyGroup> <ItemGroup> <Reference Include=""System"" /> </ItemGroup> <Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" /> </Project>"; var project = new Microsoft.Build.Evaluation.Project( new System.Xml.XmlTextReader(new System.IO.StringReader(projectCode)), null, "4.0", new Microsoft.Build.Evaluation.ProjectCollection()); // warm up the XSHD loader ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinition("C#"); // warm up the C# parser var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser(); var cu = parser.Parse(new ICSharpCode.AvalonEdit.Document.TextDocument(@"using System; class Test { int SomeMethod(string a); void Main(string[] b) { SomeMethod(b[0 + 1]); } }"), "test.cs"); // warm up the type system var unresolvedFile = cu.ToTypeSystem(); var pc = new ICSharpCode.NRefactory.CSharp.CSharpProjectContent().AddOrUpdateFiles(unresolvedFile); pc = pc.AddAssemblyReferences(ICSharpCode.NRefactory.TypeSystem.Implementation.MinimalCorlib.Instance); var compilation = pc.CreateCompilation(); // warm up the resolver var resolver = new ICSharpCode.NRefactory.CSharp.Resolver.CSharpAstResolver(compilation, cu, unresolvedFile); foreach (var node in cu.Descendants) { resolver.Resolve(node); } // load CSharp.Refactoring.dll new RedundantUsingDirectiveIssue(); // warm up AvalonEdit (must be done on main thread) SD.MainThread.InvokeAsyncAndForget( delegate { object editor; SD.EditorControlService.CreateEditor(out editor); LoggingService.Debug("Preload-Thread finished."); }, DispatcherPriority.ApplicationIdle); }
protected void Initialize() { // TODO: Unify getting auto complete data IEnumerable <CompletionData> completionData = null; int startPosition, wordLength = 0; string word = string.Empty; string filename = "filename.csx"; int offset = ScriptStart.Length; string newSourceCode = CorrectSource(""); var document = new ICSharpCode.NRefactory.Editor.ReadOnlyDocument(new ICSharpCode.NRefactory.Editor.StringTextSource(newSourceCode), filename); var syntaxTree = new ICSharpCode.NRefactory.CSharp.CSharpParser().Parse(document, document.FileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); var projectContent = this.projectContent.AddOrUpdateFiles(unresolvedFile); var compilation = projectContent.CreateCompilation(); var location = document.GetLocation(offset); var resolver = unresolvedFile.GetResolver(compilation, location); var typeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(compilation, location); var completionContextProvider = new ICSharpCode.NRefactory.CSharp.Completion.DefaultCompletionContextProvider(document, unresolvedFile); var completionDataFactory = new CompletionDataFactory(tooltipTextColor); var cce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpCompletionEngine(document, completionContextProvider, completionDataFactory, projectContent, typeResolveContextAtCaret); cce.EolMarker = Environment.NewLine; cce.FormattingPolicy = ICSharpCode.NRefactory.CSharp.FormattingOptionsFactory.CreateSharpDevelop(); var completionChar = document.GetCharAt(offset - 1); if (!cce.TryGetCompletionWord(offset, out startPosition, out wordLength)) { startPosition = offset; wordLength = 0; } completionData = cce.GetCompletionData(startPosition, true).Cast <CompletionData>(); if (wordLength > 0) { word = document.GetText(offset - wordLength, wordLength); } var parameterCompletionDataFactory = new ParameterCompletionDataFactory(tooltipTextColor); var pce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpParameterCompletionEngine( document, completionContextProvider, parameterCompletionDataFactory, projectContent, typeResolveContextAtCaret ); var parameterDataProvider = pce.GetParameterDataProvider(offset, completionChar); }
void CreateCSharpParsedDocument() { var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser(); ICSharpCode.NRefactory.CSharp.SyntaxTree unit; csharpCode = CreateCodeFile(); using (var sr = new StringReader(csharpCode)) { unit = parser.Parse(sr, "Generated.cs"); } unit.Freeze(); var parsedDoc = unit.ToTypeSystem(); parsedCodeFile = new ParsedDocumentDecorator(parsedDoc) { Ast = unit }; }
static void FormattingTest() { var code = @"using System; class Test { public static void Main(string[] args) { if (args != null ) { } } }"; var option = ICSharpCode.NRefactory.CSharp.FormattingOptionsFactory.CreateAllman(); //option.BlankLinesAfterUsings = 2; //BraceStyle.NextLine //option.SpaceWithinMethodCallParentheses = true; //option.BlankLinesBeforeFirstDeclaration = 0; //option.BlankLinesBetweenTypes = 1; //option.BlankLinesBetweenFields = 0; //option.BlankLinesBetweenEventFields = 0; //option.BlankLinesBetweenMembers = 1; //option.BlankLinesInsideRegion = 1; //option.InterfaceBraceStyle = BraceStyle.NextLineShifted; var syntaxTree = new ICSharpCode.NRefactory.CSharp.CSharpParser().Parse(code, "test.cs"); var newCode = syntaxTree.GetText(option); //var document = new StringBuilderDocument(code); //var formattingOptions = FormattingOptionsFactory.CreateAllman(); //var options = new TextEditorOptions(); //using (var script = new DocumentScript(document, formattingOptions, options)) //{ // foreach (InvocationExpression expr in file.IndexOfInvocations) // { // var copy = (InvocationExpression)expr.Clone(); // copy.Arguments.Add(new IdentifierExpression("StringComparison").Member("Ordinal")); // script.Replace(expr, copy); // } //} //var newCode = document.Text; }
void PreloadThread() { // Pre-load some stuff to make SharpDevelop more responsive once it is started. LoggingService.Debug("Preload-Thread started."); // warm up MSBuild string projectCode = @" <Project DefaultTargets=""Build"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" ToolsVersion=""4.0""> <PropertyGroup> <Configuration>Debug</Configuration> <Platform>AnyCPU</Platform> </PropertyGroup> <ItemGroup> <Reference Include=""System"" /> </ItemGroup> <Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" /> </Project>"; var project = new Microsoft.Build.Evaluation.Project( new System.Xml.XmlTextReader(new System.IO.StringReader(projectCode)), null, "4.0", new Microsoft.Build.Evaluation.ProjectCollection()); // warm up the XSHD loader ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinition("C#"); // warm up the C# parser var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser(); var cu = parser.Parse(new ICSharpCode.AvalonEdit.Document.TextDocument(@"using System; class Test { int SomeMethod(string a); void Main(string[] b) { SomeMethod(b[0 + 1]); } }"), "test.cs"); // warm up the type system var unresolvedFile = cu.ToTypeSystem(); var pc = new ICSharpCode.NRefactory.CSharp.CSharpProjectContent().AddOrUpdateFiles(unresolvedFile); pc = pc.AddAssemblyReferences(ICSharpCode.NRefactory.TypeSystem.Implementation.MinimalCorlib.Instance); var compilation = pc.CreateCompilation(); // warm up the resolver var resolver = new ICSharpCode.NRefactory.CSharp.Resolver.CSharpAstResolver(compilation, cu, unresolvedFile); foreach (var node in cu.Descendants) { resolver.Resolve(node); } // load CSharp.Refactoring.dll new RedundantUsingDirectiveIssue(); // warm up AvalonEdit (must be done on main thread) SD.MainThread.InvokeAsyncAndForget( delegate { object editor; SD.EditorControlService.CreateEditor(out editor); LoggingService.Debug("Preload-Thread finished."); }, DispatcherPriority.ApplicationIdle); }
void CreateCSharpParsedDocument () { var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser (); ICSharpCode.NRefactory.CSharp.SyntaxTree unit; csharpCode = CreateCodeFile (); using (var sr = new StringReader (csharpCode)) { unit = parser.Parse (sr, "Generated.cs"); } unit.Freeze (); var parsedDoc = unit.ToTypeSystem (); parsedCodeFile = new ParsedDocumentDecorator (parsedDoc) { Ast = unit }; }
private void ShowAutoComplete(bool autoOpening, string text) { if (autoCompletePopup == null) { IEnumerable <CompletionData> completionData = null; int startPosition, wordLength = 0; string word = string.Empty; try { string filename = "filename.csx"; int offset = CaretOffset + ScriptStart.Length; string newSourceCode = CorrectSource(Text); var document = new ICSharpCode.NRefactory.Editor.ReadOnlyDocument(new ICSharpCode.NRefactory.Editor.StringTextSource(newSourceCode), filename); var syntaxTree = new ICSharpCode.NRefactory.CSharp.CSharpParser().Parse(document, document.FileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); var projectContent = this.projectContent.AddOrUpdateFiles(unresolvedFile); var compilation = projectContent.CreateCompilation(); var location = document.GetLocation(offset); var resolver = unresolvedFile.GetResolver(compilation, location); var typeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(compilation, location); var completionContextProvider = new ICSharpCode.NRefactory.CSharp.Completion.DefaultCompletionContextProvider(document, unresolvedFile); var completionDataFactory = new CompletionDataFactory(tooltipTextColor); var cce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpCompletionEngine(document, completionContextProvider, completionDataFactory, projectContent, typeResolveContextAtCaret); cce.EolMarker = Environment.NewLine; cce.FormattingPolicy = ICSharpCode.NRefactory.CSharp.FormattingOptionsFactory.CreateSharpDevelop(); var completionChar = document.GetCharAt(offset - 1); if (!autoOpening) { if (!cce.TryGetCompletionWord(offset, out startPosition, out wordLength)) { startPosition = offset; wordLength = 0; } completionData = cce.GetCompletionData(startPosition, true).Cast <CompletionData>(); } else { startPosition = offset; if (char.IsLetterOrDigit(completionChar) || completionChar == '_') { if (!(startPosition > 1 && char.IsLetterOrDigit(document.GetCharAt(startPosition - 2)))) { completionData = cce.GetCompletionData(startPosition, false).Cast <CompletionData>(); startPosition--; wordLength = 1; } } else { completionData = cce.GetCompletionData(startPosition, false).Cast <CompletionData>(); wordLength = 0; } } if (wordLength > 0) { word = document.GetText(offset - wordLength, wordLength); } if (functionCallPopup == null) { var parameterCompletionDataFactory = new ParameterCompletionDataFactory(tooltipTextColor); var pce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpParameterCompletionEngine( document, completionContextProvider, parameterCompletionDataFactory, projectContent, typeResolveContextAtCaret ); var parameterDataProvider = pce.GetParameterDataProvider(offset, completionChar); if (functionCallPopup == null && parameterDataProvider != null) { var ppd = parameterDataProvider as ParameterDataProvider; if (ppd.Methods.Length > 0) { functionCallPopup = new OverloadInsightWindow(TextArea); functionCallPopup.Provider = ppd; if (tooltipTextBackground != null) { functionCallPopup.Background = tooltipTextBackground; } if (tooltipTextColor != null) { functionCallPopup.Foreground = tooltipTextColor; } functionCallPopup.Show(); functionCallPopup.Closed += (o, args) => functionCallPopup = null; } return; } } } catch { } if (completionData != null && completionData.Any()) { autoCompletePopup = new CompletionWindow(TextArea); System.Windows.Shell.WindowChrome.SetWindowChrome( autoCompletePopup, new System.Windows.Shell.WindowChrome() { CaptionHeight = 0, }); if (completionTextBackground != null) { autoCompletePopup.CompletionList.ListBox.Background = completionTextBackground; } if (completionTextColor != null) { autoCompletePopup.CompletionList.ListBox.Foreground = completionTextColor; } autoCompletePopup.ResizeMode = System.Windows.ResizeMode.NoResize; autoCompletePopup.Width = 0; autoCompletePopup.MinWidth = 300; autoCompletePopup.SizeToContent = System.Windows.SizeToContent.WidthAndHeight; autoCompletePopup.CloseWhenCaretAtBeginning = true; autoCompletePopup.StartOffset -= wordLength; var data = autoCompletePopup.CompletionList.CompletionData; if (completionData != null) { foreach (var cd in completionData.GroupBy(c => c.Content).Select(g => g.OrderBy(c => c.CompletionText).First()).OrderBy(c => c.Content)) { data.Add(cd); } if (wordLength > 0) { autoCompletePopup.CompletionList.SelectItem(word); } } autoCompletePopup.Show(); autoCompletePopup.Closed += (o, args) => autoCompletePopup = null; } } if (!string.IsNullOrEmpty(text) && functionCallPopup != null) { var provider = functionCallPopup.Provider as ParameterDataProvider; if (provider != null) { string filename = "filename.csx"; int offset = CaretOffset + ScriptStart.Length; string newSourceCode = CorrectSource(Text); var document = new ICSharpCode.NRefactory.Editor.ReadOnlyDocument(new ICSharpCode.NRefactory.Editor.StringTextSource(newSourceCode), filename); var syntaxTree = new ICSharpCode.NRefactory.CSharp.CSharpParser().Parse(document, document.FileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); var projectContent = this.projectContent.AddOrUpdateFiles(unresolvedFile); var compilation = projectContent.CreateCompilation(); var location = document.GetLocation(offset); var typeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(compilation, location); var completionContextProvider = new ICSharpCode.NRefactory.CSharp.Completion.DefaultCompletionContextProvider(document, unresolvedFile); var parameterCompletionDataFactory = new ParameterCompletionDataFactory(tooltipTextColor); var completionChar = document.GetCharAt(offset - 1); var pce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpParameterCompletionEngine( document, completionContextProvider, parameterCompletionDataFactory, projectContent, typeResolveContextAtCaret ); int parameterIndex = pce.GetCurrentParameterIndex(provider.StartOffset, offset); if (parameterIndex < 0) { functionCallPopup.Close(); functionCallPopup = null; } else { provider.CurrentParameter = parameterIndex - 1; } } } }
protected void Initialize() { // TODO: Unify getting auto complete data IEnumerable<CompletionData> completionData = null; int startPosition, wordLength = 0; string word = string.Empty; string filename = "filename.csx"; int offset = ScriptStart.Length; string newSourceCode = CorrectSource(""); var document = new ICSharpCode.NRefactory.Editor.ReadOnlyDocument(new ICSharpCode.NRefactory.Editor.StringTextSource(newSourceCode), filename); var syntaxTree = new ICSharpCode.NRefactory.CSharp.CSharpParser().Parse(document, document.FileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); var projectContent = this.projectContent.AddOrUpdateFiles(unresolvedFile); var compilation = projectContent.CreateCompilation(); var location = document.GetLocation(offset); var resolver = unresolvedFile.GetResolver(compilation, location); var typeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(compilation, location); var completionContextProvider = new ICSharpCode.NRefactory.CSharp.Completion.DefaultCompletionContextProvider(document, unresolvedFile); var completionDataFactory = new CompletionDataFactory(); var cce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpCompletionEngine(document, completionContextProvider, completionDataFactory, projectContent, typeResolveContextAtCaret); cce.EolMarker = Environment.NewLine; cce.FormattingPolicy = ICSharpCode.NRefactory.CSharp.FormattingOptionsFactory.CreateSharpDevelop(); var completionChar = document.GetCharAt(offset - 1); if (!cce.TryGetCompletionWord(offset, out startPosition, out wordLength)) { startPosition = offset; wordLength = 0; } completionData = cce.GetCompletionData(startPosition, true).Cast<CompletionData>(); if (wordLength > 0) word = document.GetText(offset - wordLength, wordLength); var parameterCompletionDataFactory = new ParameterCompletionDataFactory(); var pce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpParameterCompletionEngine( document, completionContextProvider, parameterCompletionDataFactory, projectContent, typeResolveContextAtCaret ); var parameterDataProvider = pce.GetParameterDataProvider(offset, completionChar); }
private void ShowAutoComplete(bool autoOpening, string text) { if (autoCompletePopup == null) { IEnumerable<CompletionData> completionData = null; int startPosition, wordLength = 0; string word = string.Empty; try { string filename = "filename.csx"; int offset = CaretOffset + ScriptStart.Length; string newSourceCode = CorrectSource(Text); var document = new ICSharpCode.NRefactory.Editor.ReadOnlyDocument(new ICSharpCode.NRefactory.Editor.StringTextSource(newSourceCode), filename); var syntaxTree = new ICSharpCode.NRefactory.CSharp.CSharpParser().Parse(document, document.FileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); var projectContent = this.projectContent.AddOrUpdateFiles(unresolvedFile); var compilation = projectContent.CreateCompilation(); var location = document.GetLocation(offset); var resolver = unresolvedFile.GetResolver(compilation, location); var typeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(compilation, location); var completionContextProvider = new ICSharpCode.NRefactory.CSharp.Completion.DefaultCompletionContextProvider(document, unresolvedFile); var completionDataFactory = new CompletionDataFactory(); var cce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpCompletionEngine(document, completionContextProvider, completionDataFactory, projectContent, typeResolveContextAtCaret); cce.EolMarker = Environment.NewLine; cce.FormattingPolicy = ICSharpCode.NRefactory.CSharp.FormattingOptionsFactory.CreateSharpDevelop(); var completionChar = document.GetCharAt(offset - 1); if (!autoOpening) { if (!cce.TryGetCompletionWord(offset, out startPosition, out wordLength)) { startPosition = offset; wordLength = 0; } completionData = cce.GetCompletionData(startPosition, true).Cast<CompletionData>(); } else { startPosition = offset; if (char.IsLetterOrDigit(completionChar) || completionChar == '_') { if (!(startPosition > 1 && char.IsLetterOrDigit(document.GetCharAt(startPosition - 2)))) { completionData = cce.GetCompletionData(startPosition, false).Cast<CompletionData>(); startPosition--; wordLength = 1; } } else { completionData = cce.GetCompletionData(startPosition, false).Cast<CompletionData>(); wordLength = 0; } } if (wordLength > 0) word = document.GetText(offset - wordLength, wordLength); if (functionCallPopup == null) { var parameterCompletionDataFactory = new ParameterCompletionDataFactory(); var pce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpParameterCompletionEngine( document, completionContextProvider, parameterCompletionDataFactory, projectContent, typeResolveContextAtCaret ); var parameterDataProvider = pce.GetParameterDataProvider(offset, completionChar); if (functionCallPopup == null && parameterDataProvider != null) { var ppd = parameterDataProvider as ParameterDataProvider; if (ppd.Methods.Length > 0) { functionCallPopup = new OverloadInsightWindow(TextArea); functionCallPopup.Provider = ppd; functionCallPopup.Show(); functionCallPopup.Closed += (o, args) => functionCallPopup = null; } return; } } } catch { } if (completionData != null && completionData.Any()) { autoCompletePopup = new CompletionWindow(TextArea); autoCompletePopup.CloseWhenCaretAtBeginning = true; autoCompletePopup.StartOffset -= wordLength; var data = autoCompletePopup.CompletionList.CompletionData; if (completionData != null) { foreach (var cd in completionData.GroupBy(c => c.Content).Select(g => g.OrderBy(c => c.CompletionText).First()).OrderBy(c => c.Content)) { data.Add(cd); } if (wordLength > 0) { autoCompletePopup.CompletionList.SelectItem(word); } } autoCompletePopup.Show(); autoCompletePopup.Closed += (o, args) => autoCompletePopup = null; } } if (!string.IsNullOrEmpty(text) && functionCallPopup != null) { var provider = functionCallPopup.Provider as ParameterDataProvider; if (provider != null) { string filename = "filename.csx"; int offset = CaretOffset + ScriptStart.Length; string newSourceCode = CorrectSource(Text); var document = new ICSharpCode.NRefactory.Editor.ReadOnlyDocument(new ICSharpCode.NRefactory.Editor.StringTextSource(newSourceCode), filename); var syntaxTree = new ICSharpCode.NRefactory.CSharp.CSharpParser().Parse(document, document.FileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); var projectContent = this.projectContent.AddOrUpdateFiles(unresolvedFile); var compilation = projectContent.CreateCompilation(); var location = document.GetLocation(offset); var typeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(compilation, location); var completionContextProvider = new ICSharpCode.NRefactory.CSharp.Completion.DefaultCompletionContextProvider(document, unresolvedFile); var parameterCompletionDataFactory = new ParameterCompletionDataFactory(); var completionChar = document.GetCharAt(offset - 1); var pce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpParameterCompletionEngine( document, completionContextProvider, parameterCompletionDataFactory, projectContent, typeResolveContextAtCaret ); int parameterIndex = pce.GetCurrentParameterIndex(provider.StartOffset, offset); if (parameterIndex < 0) { functionCallPopup.Close(); functionCallPopup = null; } else { provider.CurrentParameter = parameterIndex - 1; } } } }