protected IEnumerable<MonoDevelop.CodeActions.CodeAction> GetActions (MDRefactoringContext context)
		{
			if (context.IsInvalid)
				yield break;
			var type = GetTypeDeclaration (context);
			if (type == null)
				yield break;
			if (Path.GetFileNameWithoutExtension (context.Document.FileName) == type.Name)
				yield break;
			string title;
			if (IsSingleType (context)) {
				title = String.Format (GettextCatalog.GetString ("_Rename file to '{0}'"), Path.GetFileName (GetCorrectFileName (context, type)));
			} else {
				title = String.Format (GettextCatalog.GetString ("_Move type to file '{0}'"), Path.GetFileName (GetCorrectFileName (context, type)));
			}
			yield return new MonoDevelop.CodeActions.DefaultCodeAction (title, (d, l) => {
				var ctx = new MDRefactoringContext (d, l);
				string correctFileName = GetCorrectFileName (ctx, type);
				if (IsSingleType (ctx)) {
					FileService.RenameFile (ctx.Document.FileName, correctFileName);
					if (ctx.Document.Project != null)
						ctx.Document.Project.Save (new NullProgressMonitor ());
					return;
				}
				
				CreateNewFile (ctx, type, correctFileName);
				using (var script = ctx.StartScript ()) {
					script.Remove (type);
				}
			});
		}
		public MDRefactoringScript (MDRefactoringContext context, Document document, CSharpFormattingOptions formattingOptions) : base(document.Editor.Document, formattingOptions, document.Editor.CreateNRefactoryTextEditorOptions ())
		{
			this.context = context;
			this.document = document;
			undoGroup  = this.document.Editor.OpenUndoGroup ();
			this.startVersion = this.document.Editor.Version;

		}
Exemple #3
0
        public override IEnumerable <CodeIssue> GetIssues(Document document, CancellationToken cancellationToken)
        {
            var context = new MDRefactoringContext(document, document.Editor.Caret.Location, cancellationToken);

            if (context.IsInvalid || context.RootNode == null)
            {
                yield break;
            }
            int issueNum = 0;

            foreach (var action in issueProvider.GetIssues(context))
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    yield break;
                }
                if (action.Actions == null)
                {
                    LoggingService.LogError("NRefactory actions == null in :" + Title);
                    continue;
                }
                if (actionIdList.Count <= issueNum)
                {
                    actionIdList.Add(new List <string> ());
                }
                var actionId  = actionIdList [issueNum];
                int actionNum = 0;

                var actions = new List <MonoDevelop.CodeActions.CodeAction> ();
                foreach (var act in action.Actions)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        yield break;
                    }
                    if (act == null)
                    {
                        LoggingService.LogError("NRefactory issue action was null in :" + Title);
                        continue;
                    }
                    if (actionId.Count <= actionNum)
                    {
                        actionId.Add(issueProvider.GetType().FullName + "'" + issueNum + "'" + actionNum);
                    }
                    actions.Add(new NRefactoryCodeAction(actionId[actionNum], act.Description, act));
                    actionNum++;
                }
                var issue = new CodeIssue(
                    GettextCatalog.GetString(action.Description ?? ""),
                    action.Start,
                    action.End,
                    actions
                    );
                yield return(issue);

                issueNum++;
            }
        }
		static void CreateNewFile (MDRefactoringContext context, TypeDeclaration type, string correctFileName)
		{
			var content = context.Document.Editor.Text;
			
			var types = new List<EntityDeclaration> (context.Unit.GetTypes ().Where (t => t.StartLocation != type.StartLocation));
			types.Sort ((x, y) => y.StartLocation.CompareTo (x.StartLocation));

			foreach (var removeType in types) {
				var start = context.GetOffset (removeType.StartLocation);
				var end = context.GetOffset (removeType.EndLocation);
				content = content.Remove (start, end - start);
			}
			
			if (context.Document.Project is MonoDevelop.Projects.DotNetProject) {
				string header = StandardHeaderService.GetHeader (context.Document.Project, correctFileName, true);
				if (!string.IsNullOrEmpty (header))
					content = header + context.Document.Editor.EolMarker + StripHeader (content);
			}
			content = StripDoubleBlankLines (content);
			
			File.WriteAllText (correctFileName, content);
			context.Document.Project.AddFile (correctFileName);
			MonoDevelop.Ide.IdeApp.ProjectOperations.Save (context.Document.Project);
		}
        public override ParsedDocument Parse(bool storeAst, string fileName, System.IO.TextReader content, MonoDevelop.Projects.Project project = null)
        {
            var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser(GetCompilerArguments(project));

            parser.GenerateTypeSystemMode = !storeAst;
            var result = new ParsedDocumentDecorator();

            if (project != null)
            {
                var projectFile = project.Files.GetFile(fileName);
                if (projectFile != null && !TypeSystemParserNode.IsCompileBuildAction(projectFile.BuildAction))
                {
                    result.Flags |= ParsedDocumentFlags.NonSerializable;
                }
            }

            var tagComments = CommentTag.SpecialCommentTags.Select(t => t.Tag).ToArray();

            parser.CompilationUnitCallback = delegate(CompilerCompilationUnit top) {
                foreach (var special in top.SpecialsBag.Specials)
                {
                    var comment = special as SpecialsBag.Comment;
                    if (comment != null)
                    {
                        VisitComment(result, comment, tagComments);
                    }
                    else
                    {
                        if (storeAst)
                        {
                            var ppd = special as SpecialsBag.PreProcessorDirective;
                            if (ppd != null)
                            {
                                VisitPreprocessorDirective(result, ppd);
                            }
                        }
                    }
                }
            };

            var unit = parser.Parse(content, fileName);

            unit.Freeze();
            var pf = unit.ToTypeSystem();

            try {
                pf.LastWriteTime = System.IO.File.GetLastWriteTimeUtc(fileName);
            } catch (Exception) {
                pf.LastWriteTime = DateTime.UtcNow;
            }

            result.LastWriteTimeUtc = pf.LastWriteTime.Value;
            result.ParsedFile       = pf;
            result.Add(GetSemanticTags(unit));

            result.CreateRefactoringContext = delegate(MonoDevelop.Ide.Gui.Document doc, System.Threading.CancellationToken token) {
                var task = MDRefactoringContext.Create(doc, doc.Editor.Caret.Location, token);
                task.Wait(5000, token);
                if (!task.IsCompleted)
                {
                    return(null);
                }
                return(task.Result);
            };
            result.CreateRefactoringContextWithEditor = (data, resolver, token) => new MDRefactoringContext((DotNetProject)project, data, result, (CSharpAstResolver)resolver, TextLocation.Empty, token);

            if (storeAst)
            {
                result.Ast = unit;
                result.Add(GenerateFoldings(unit, result));
            }
            return(result);
        }
        internal IEnumerable <CodeIssue> ToMonoDevelopRepresentation(CancellationToken cancellationToken, MDRefactoringContext context, IEnumerable <ICSharpCode.NRefactory.CSharp.Refactoring.CodeIssue> issues)
        {
            var actionGroups = new Dictionary <object, IList <ICSharpCode.NRefactory.CSharp.Refactoring.CodeAction> > ();

            foreach (var issue in issues)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    yield break;
                }
                if (issue.Actions == null)
                {
                    LoggingService.LogError("NRefactory actions == null in :" + Title);
                    continue;
                }
                var actions = new List <NRefactoryCodeAction> ();
                foreach (var act in GetActions(issue, context))
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        yield break;
                    }
                    if (act == null)
                    {
                        LoggingService.LogError("NRefactory issue action was null in :" + Title);
                        continue;
                    }
                    var nrefactoryCodeAction = new NRefactoryCodeAction(IdString, act.Description, act, act.SiblingKey);
                    if (act.SiblingKey != null)
                    {
                        // make sure the action has a list of its siblings
                        IList <ICSharpCode.NRefactory.CSharp.Refactoring.CodeAction> siblingGroup;
                        if (!actionGroups.TryGetValue(act.SiblingKey, out siblingGroup))
                        {
                            siblingGroup = new List <ICSharpCode.NRefactory.CSharp.Refactoring.CodeAction> ();
                            actionGroups.Add(act.SiblingKey, siblingGroup);
                        }
                        siblingGroup.Add(act);
                        nrefactoryCodeAction.SiblingActions = siblingGroup;
                    }
                    actions.Add(nrefactoryCodeAction);
                }
                yield return(new CodeIssue(issue.IssueMarker, GettextCatalog.GetString(issue.Description ?? ""), context.TextEditor.FileName, issue.Start, issue.End, IdString, actions));
            }
        }
 IEnumerable <ICSharpCode.NRefactory.CSharp.Refactoring.CodeAction> GetActions(ICSharpCode.NRefactory.CSharp.Refactoring.CodeIssue issue, MDRefactoringContext context)
 {
     foreach (var action in issue.Actions)
     {
         yield return(action);
     }
     if (boundActionProvider != null)
     {
         // We need to se the correct location here. Seems very fragile to do so...
         context.SetLocation(issue.Start);
         foreach (var action in boundActionProvider.GetActions(context))
         {
             yield return(action);
         }
     }
 }
 public MonoCSharpCompletionEngine(CSharpCompletionTextEditorExtension ext, ICSharpCode.NRefactory.Editor.IDocument document, ICompletionContextProvider completionContextProvider, ICompletionDataFactory factory, ICSharpCode.NRefactory.TypeSystem.IProjectContent content, ICSharpCode.NRefactory.CSharp.TypeSystem.CSharpTypeResolveContext ctx) : base(document, completionContextProvider, factory, content, ctx)
 {
     this.ext = ext;
     this.mdRefactoringCtx = MDRefactoringContext.Create(ext.Document, ext.Document.Editor.Caret.Location);
 }
		internal static string GetCorrectFileName (MDRefactoringContext context, EntityDeclaration type)
		{
			if (type == null)
				return context.Document.FileName;
			return Path.Combine (Path.GetDirectoryName (context.Document.FileName), type.Name + Path.GetExtension (context.Document.FileName));
		}
		TypeDeclaration GetTypeDeclaration (MDRefactoringContext context)
		{
			var result = context.GetNode<TypeDeclaration> ();
			if (result == null || result.Parent is TypeDeclaration)
				return null;
			if (result != null && result.NameToken.Contains (context.Location))
				return result;
			return null;
		}
		bool IsSingleType (MDRefactoringContext context)
		{
			return context.Unit.GetTypes ().Count () == 1;
		}
		public override void Run (Document document, TextLocation loc)
		{
			var context = new MDRefactoringContext (document, loc);
			using (var script = context.StartScript ())
				act.Run (script);
		}
Exemple #13
0
        IEnumerable <NRefactoryCodeAction> GetActions(ICSharpCode.NRefactory.CSharp.Refactoring.CodeIssue issue, MDRefactoringContext context)
        {
            foreach (var action in issue.Actions)
            {
                yield return(new NRefactoryCodeAction(IdString, action.Description, action, action.SiblingKey));
            }

            if (issue.ActionProvider != null)
            {
                foreach (var provider in issue.ActionProvider)
                {
                    var boundActionProvider = (ICSharpCode.NRefactory.CSharp.Refactoring.CodeActionProvider)Activator.CreateInstance(provider);
                    context.SetLocation(issue.Start);
                    foreach (var action in boundActionProvider.GetActions(context))
                    {
                        yield return(new NRefactoryCodeAction(provider.FullName, action.Description, action, action.SiblingKey));
                    }
                }
            }
        }