28
using MonoDevelop.CSharp.Resolver;
29
28
using ICSharpCode.NRefactory.CSharp;
30
using System.Collections.Generic;
31
using Mono.TextEditor;
32
29
using MonoDevelop.Projects;
33
30
using MonoDevelop.Core;
34
31
using MonoDevelop.Refactoring;
35
32
using ICSharpCode.NRefactory.CSharp.Refactoring;
36
using ICSharpCode.NRefactory.TypeSystem;
37
using ICSharpCode.NRefactory.TypeSystem.Implementation;
38
using ICSharpCode.NRefactory.CSharp.Resolver;
40
33
using MonoDevelop.Ide.TypeSystem;
41
34
using ICSharpCode.NRefactory;
42
using ICSharpCode.NRefactory.Semantics;
43
using ICSharpCode.NRefactory.CSharp.TypeSystem;
44
35
using System.Threading;
45
36
using MonoDevelop.Ide.Gui;
46
37
using System.Diagnostics;
47
38
using MonoDevelop.CSharp.Refactoring.CodeIssues;
39
using Mono.TextEditor;
40
using ICSharpCode.NRefactory.CSharp.Resolver;
41
using MonoDevelop.CSharp.Formatting;
49
43
namespace MonoDevelop.CSharp.Refactoring.CodeActions
51
45
public class MDRefactoringContext : RefactoringContext
53
public MonoDevelop.Ide.Gui.Document Document {
47
public TextEditorData TextEditor {
52
public DotNetProject Project {
58
57
public bool IsInvalid {
60
return Resolver == null;
59
if (Resolver == null || TextEditor == null)
61
return ParsedDocument == null || ParsedDocument.HasErrors;
64
public CompilationUnit Unit {
65
public ParsedDocument ParsedDocument {
70
public SyntaxTree Unit {
66
72
Debug.Assert (!IsInvalid);
67
return Document.ParsedDocument.GetAst<CompilationUnit> ();
73
return ParsedDocument.GetAst<SyntaxTree> ();
71
77
public override bool Supports (Version version)
73
var project = Document.Project as DotNetProject;
79
var project = Project;
74
80
if (project == null)
76
82
switch (project.TargetFramework.ClrVersion) {
88
94
public override ICSharpCode.NRefactory.CSharp.TextEditorOptions TextEditorOptions {
90
return Document.Editor.CreateNRefactoryTextEditorOptions ();
96
return TextEditor.CreateNRefactoryTextEditorOptions ();
94
100
public override bool IsSomethingSelected {
96
return Document.Editor.IsSomethingSelected;
102
return TextEditor.IsSomethingSelected;
100
106
public override string SelectedText {
102
return Document.Editor.SelectedText;
108
return TextEditor.SelectedText;
106
112
public override TextLocation SelectionStart {
108
return Document.Editor.MainSelection.Start;
114
return TextEditor.MainSelection.Start;
112
118
public override TextLocation SelectionEnd {
114
return Document.Editor.MainSelection.End;
120
return TextEditor.MainSelection.End;
118
124
public override int GetOffset (TextLocation location)
120
return Document.Editor.LocationToOffset (location);
126
return TextEditor.LocationToOffset (location);
123
129
public override TextLocation GetLocation (int offset)
125
return Document.Editor.OffsetToLocation (offset);
131
return TextEditor.OffsetToLocation (offset);
128
134
public override string GetText (int offset, int length)
130
return Document.Editor.GetTextAt (offset, length);
136
return TextEditor.GetTextAt (offset, length);
133
139
public override string GetText (ICSharpCode.NRefactory.Editor.ISegment segment)
135
return Document.Editor.GetTextAt (segment.Offset, segment.Length);
141
return TextEditor.GetTextAt (segment.Offset, segment.Length);
138
144
public override ICSharpCode.NRefactory.Editor.IDocumentLine GetLineByOffset (int offset)
140
return Document.Editor.GetLineByOffset (offset);
146
return TextEditor.GetLineByOffset (offset);
143
149
readonly TextLocation location;
156
CSharpFormattingOptions formattingOptions;
150
158
public Script StartScript ()
152
return new MDRefactoringScript (this, this.Document, this.Document.GetFormattingOptions ());
156
static CSharpAstResolver CreateResolver (Document document)
158
var parsedDocument = document.ParsedDocument;
159
if (parsedDocument == null)
162
var unit = parsedDocument.GetAst<CompilationUnit> ();
163
var parsedFile = parsedDocument.ParsedFile as CSharpParsedFile;
165
if (unit == null || parsedFile == null)
168
return new CSharpAstResolver (document.Compilation, unit, parsedFile);
171
public MDRefactoringContext (MonoDevelop.Ide.Gui.Document document, TextLocation loc, CancellationToken cancellationToken = default (CancellationToken)) : base (CreateResolver (document), cancellationToken)
160
return new MDRefactoringScript (this, formattingOptions);
163
public MDRefactoringContext (Document document, TextLocation loc, CancellationToken cancellationToken = default (CancellationToken)) : base (document.GetSharedResolver ().Result, cancellationToken)
173
165
if (document == null)
174
166
throw new ArgumentNullException ("document");
175
this.Document = document;
167
this.TextEditor = document.Editor;
168
this.ParsedDocument = document.ParsedDocument;
169
this.Project = document.Project as DotNetProject;
170
this.formattingOptions = document.GetFormattingOptions ();
176
171
this.location = RefactoringService.GetCorrectResolveLocation (document, loc);
177
var policy = Document.HasProject ? Document.Project.Policies.Get<NameConventionPolicy> () : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<NameConventionPolicy> ();
172
var policy = document.HasProject ? Project.Policies.Get<NameConventionPolicy> () : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<NameConventionPolicy> ();
178
173
Services.AddService (typeof(NamingConventionService), policy.CreateNRefactoryService ());
176
public MDRefactoringContext (DotNetProject project, TextEditorData data, ParsedDocument parsedDocument, CSharpAstResolver resolver, TextLocation loc, CancellationToken cancellationToken = default (CancellationToken)) : base (resolver, cancellationToken)
178
this.TextEditor = data;
179
this.ParsedDocument = parsedDocument;
180
this.Project = project;
181
var policy = Project.Policies.Get<CSharpFormattingPolicy> ();
182
this.formattingOptions = policy.CreateOptions ();
184
var namingPolicy = Project.Policies.Get<NameConventionPolicy> ();
185
Services.AddService (typeof(NamingConventionService), namingPolicy.CreateNRefactoryService ());