public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var usingNode = FindUsingNodeAtCursor(context); if (usingNode == null) { yield break; } yield return(new CodeAction(context.TranslateString("Sort usings"), script => { var blocks = EnumerateUsingBlocks(context.RootNode); foreach (var block in blocks) { var originalNodes = block.ToArray(); var sortedNodes = UsingHelper.SortUsingBlock(originalNodes, context).ToArray(); for (var i = 0; i < originalNodes.Length; ++i) { script.Replace(originalNodes[i], sortedNodes[i].Clone()); } } }, usingNode)); }
string AddUsingLinq(AstNode astNode, string buffer) { var request = CreateRequest(buffer, astNode); var context = OmniSharpRefactoringContext.GetContext(_bufferParser, request); var script = new OmniSharpScript(context, _config); UsingHelper.InsertUsingAndRemoveRedundantNamespaceUsage(context, script, "System.Linq"); return(context.Document.Text); }
public override void AddImport(FileName fileName, string namespaceName) { var context = RefactoringExtensions.CreateRefactoringContext(new DomRegion(fileName, 0, 0)); var astBuilder = context.CreateTypeSystemAstBuilder(); using (var script = context.StartScript()) { AstType ns = astBuilder.ConvertNamespace(namespaceName); UsingHelper.InsertUsing(context, script, ns); } }
public override void Complete(CompletionContext context) { context.Editor.Document.Replace(context.StartOffset, context.Length, insertionText); context.EndOffset = context.StartOffset + insertionText.Length; if (insertUsing != null) { SD.Log.Debug("Insert using '" + insertUsing + "'"); var refactoringContext = SDRefactoringContext.Create(context.Editor, CancellationToken.None); using (var script = refactoringContext.StartScript()) { UsingHelper.InsertUsing(refactoringContext, script, new UsingDeclaration(insertUsing)); } } }
void CreateCodeImportWithNoNamespace() { helper = new UsingHelper(); codeImport = new CodeImport(helper.Using); }
void CreateCodeImport(string namespaceName) { helper = new UsingHelper(); helper.AddNamespace(namespaceName); codeImport = new CodeImport(helper.Using); }
private static async Task <InitializationResult> InitializeAsync( InitializationArgs args ) { using (var cancellationTokenSource = new CancellationTokenSource()) { var token = cancellationTokenSource.Token; void OnCancel(Object sender, ConsoleCancelEventArgs e) { cancellationTokenSource.Cancel(); } Console.CancelKeyPress += OnCancel; using (var usingHelper = new UsingHelper(() => Console.CancelKeyPress -= OnCancel)) { var be = args.BuildEngine; var projectFilePath = be?.ProjectFileOfTaskNode; var env = await _cache.DetectEnvironmentAsync(new EnvironmentKeyInfo( new EnvironmentKey( args.Framework, args.RuntimeID, args.SDKPackageID, args.SDKPackageVersion, args.SettingsLocation, args.PackageIDIsSelf ? projectFilePath : args.PackageID, args.PackageVersion ), args.PackageIDIsSelf, projectFilePath ), be, token); InitializationResult initializationResult = null; if (env.Errors.Length > 0) { if (be == null) { Console.Error.WriteLine("Errors in environment detection: " + String.Join(";", env.Errors)); } else { foreach (var error in env.Errors.Select(errorCode => errorCode.AsNuGetMSBuildError(projectFilePath, args))) { be.LogErrorEvent(error); } } initializationResult = null; } else { await be.LogMessageOrWriteToConsoleOut($"Detected current NuGet framework to be \"{env.ThisFramework}\", with RID \"{env.ThisRuntimeID}\"."); var inspection = await _cache.InspectPackageAsync(env, new InspectionKey( env.ThisFramework, args.SettingsLocation, env.PackageID, env.PackageVersion, args.AssemblyPath ), args.RestoreSDKPackage, be, token ); var epType = args.TypeName; var epMethod = args.MethodName; if (epType.IsNullOrEmpty() && epMethod.IsNullOrEmpty()) { // TODO load the assembly using System.Reflection.Metadata stuff and inspect it. // OR just invoke the process which will use the existing stuff in NuGetUtils.Lib.Exec throw new NotImplementedException("The scenario where both type and method names of entrypoint are not specified is not yet supported."); } Func <MethodInspectionInfo, Boolean> matcher; if (epType.IsNullOrEmpty()) { // Get the first method matching given method name matcher = m => String.Equals(m.MethodName, epMethod); } else { if (epMethod.IsNullOrEmpty()) { // Get the first method contained within given type matcher = m => String.Equals(m.TypeName, epType); } else { // Get the first method matching given method name which is contained by type with given type name matcher = m => String.Equals(m.MethodName, epMethod) && String.Equals(m.TypeName, epType); } } var epInfo = inspection.SuitableMethods.FirstOrDefault(matcher); if (epInfo == null) { throw new InvalidOperationException($"Could not find suitable method with the following information: entrypoint type {epType}, and entrypoing method {epMethod}."); } var typeGenResult = TaskTypeGenerator.Instance.GenerateTaskType( true, epInfo.InputParameters, epInfo.OutputParameters ); initializationResult = new InitializationResult( typeGenResult, () => (ITask)typeGenResult.GeneratedType.GetTypeInfo().DeclaredConstructors.First().Invoke(new[] { new TaskProxy(_cache.ProcessMonitor, args, env, inspection, epInfo, typeGenResult) }) ); } return(initializationResult); } } }
CodeAction NewUsingAction(RefactoringContext context, AstNode node, string ns) { return(new CodeAction("include " + ns + ";", s => UsingHelper.InsertUsingAndRemoveRedundantNamespaceUsage(context, s, ns), node)); }
public void AddNamespaceAlias(string alias, string namespaceName) { var usingHelper = new UsingHelper(); usingHelper.AddNamespaceAlias(alias, namespaceName); Usings.Add(usingHelper.Using); }
public void AddNamespace(string name) { var usingHelper = new UsingHelper(); usingHelper.AddNamespace(name); Usings.Add(usingHelper.Using); }
public BuilderBase(string @namespace) { _memberHelper = new MemberHelper(); _usingHelper = new UsingHelper(); _namespaceHelper = new NamespaceHelper(@namespace); }