~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/CSharpBinding/MonoDevelop.CSharp.Refactoring.CodeActions/MDRefactoringContext.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
// THE SOFTWARE.
26
26
 
27
27
using System;
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;
39
 
using System.Linq;
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;
48
42
 
49
43
namespace MonoDevelop.CSharp.Refactoring.CodeActions
50
44
{
51
45
        public class MDRefactoringContext : RefactoringContext
52
46
        {
53
 
                public MonoDevelop.Ide.Gui.Document Document {
 
47
                public TextEditorData TextEditor {
 
48
                        get;
 
49
                        private set;
 
50
                }
 
51
 
 
52
                public DotNetProject Project {
54
53
                        get;
55
54
                        private set;
56
55
                }
57
56
 
58
57
                public bool IsInvalid {
59
58
                        get {
60
 
                                return Resolver == null;
 
59
                                if (Resolver == null || TextEditor == null)
 
60
                                        return true;
 
61
                                return ParsedDocument == null || ParsedDocument.HasErrors;
61
62
                        }
62
63
                }
63
64
 
64
 
                public CompilationUnit Unit {
 
65
                public ParsedDocument ParsedDocument {
 
66
                        get;
 
67
                        private set;
 
68
                }
 
69
 
 
70
                public SyntaxTree Unit {
65
71
                        get {
66
72
                                Debug.Assert (!IsInvalid);
67
 
                                return Document.ParsedDocument.GetAst<CompilationUnit> ();
 
73
                                return ParsedDocument.GetAst<SyntaxTree> ();
68
74
                        }
69
75
                }
70
76
 
71
77
                public override bool Supports (Version version)
72
78
                {
73
 
                        var project = Document.Project as DotNetProject;
 
79
                        var project = Project;
74
80
                        if (project == null)
75
81
                                return true;
76
82
                        switch (project.TargetFramework.ClrVersion) {
87
93
 
88
94
                public override ICSharpCode.NRefactory.CSharp.TextEditorOptions TextEditorOptions {
89
95
                        get {
90
 
                                return Document.Editor.CreateNRefactoryTextEditorOptions ();
 
96
                                return TextEditor.CreateNRefactoryTextEditorOptions ();
91
97
                        }
92
98
                }
93
99
                
94
100
                public override bool IsSomethingSelected { 
95
101
                        get {
96
 
                                return Document.Editor.IsSomethingSelected;
 
102
                                return TextEditor.IsSomethingSelected;
97
103
                        }
98
104
                }
99
105
                
100
106
                public override string SelectedText {
101
107
                        get {
102
 
                                return Document.Editor.SelectedText;
 
108
                                return TextEditor.SelectedText;
103
109
                        }
104
110
                }
105
111
                
106
112
                public override TextLocation SelectionStart {
107
113
                        get {
108
 
                                return Document.Editor.MainSelection.Start;
 
114
                                return TextEditor.MainSelection.Start;
109
115
                        }
110
116
                }
111
117
                
112
118
                public override TextLocation SelectionEnd { 
113
119
                        get {
114
 
                                return Document.Editor.MainSelection.End;
 
120
                                return TextEditor.MainSelection.End;
115
121
                        }
116
122
                }
117
123
 
118
124
                public override int GetOffset (TextLocation location)
119
125
                {
120
 
                        return Document.Editor.LocationToOffset (location);
 
126
                        return TextEditor.LocationToOffset (location);
121
127
                }
122
128
                
123
129
                public override TextLocation GetLocation (int offset)
124
130
                {
125
 
                        return Document.Editor.OffsetToLocation (offset);
 
131
                        return TextEditor.OffsetToLocation (offset);
126
132
                }
127
133
 
128
134
                public override string GetText (int offset, int length)
129
135
                {
130
 
                        return Document.Editor.GetTextAt (offset, length);
 
136
                        return TextEditor.GetTextAt (offset, length);
131
137
                }
132
138
                
133
139
                public override string GetText (ICSharpCode.NRefactory.Editor.ISegment segment)
134
140
                {
135
 
                        return Document.Editor.GetTextAt (segment.Offset, segment.Length);
 
141
                        return TextEditor.GetTextAt (segment.Offset, segment.Length);
136
142
                }
137
143
                
138
144
                public override ICSharpCode.NRefactory.Editor.IDocumentLine GetLineByOffset (int offset)
139
145
                {
140
 
                        return Document.Editor.GetLineByOffset (offset);
 
146
                        return TextEditor.GetLineByOffset (offset);
141
147
                }
142
148
 
143
149
                readonly TextLocation location;
147
153
                        }
148
154
                }
149
155
 
 
156
                CSharpFormattingOptions formattingOptions;
 
157
 
150
158
                public Script StartScript ()
151
159
                {
152
 
                        return new MDRefactoringScript (this, this.Document, this.Document.GetFormattingOptions ());
153
 
                }
154
 
 
155
 
 
156
 
                static CSharpAstResolver CreateResolver (Document document)
157
 
                {
158
 
                        var parsedDocument = document.ParsedDocument;
159
 
                        if (parsedDocument == null)
160
 
                                return null;
161
 
 
162
 
                        var unit       = parsedDocument.GetAst<CompilationUnit> ();
163
 
                        var parsedFile = parsedDocument.ParsedFile as CSharpParsedFile;
164
 
                        
165
 
                        if (unit == null || parsedFile == null)
166
 
                                return null;
167
 
 
168
 
                        return new CSharpAstResolver (document.Compilation, unit, parsedFile);
169
 
                }
170
 
 
171
 
                public MDRefactoringContext (MonoDevelop.Ide.Gui.Document document, TextLocation loc, CancellationToken cancellationToken = default (CancellationToken)) : base (CreateResolver (document), cancellationToken)
 
160
                        return new MDRefactoringScript (this, formattingOptions);
 
161
                }
 
162
 
 
163
                public MDRefactoringContext (Document document, TextLocation loc, CancellationToken cancellationToken = default (CancellationToken)) : base (document.GetSharedResolver ().Result, cancellationToken)
172
164
                {
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 ());
179
 
 
180
 
                }
181
 
 
 
174
                }
 
175
 
 
176
                public MDRefactoringContext (DotNetProject project, TextEditorData data, ParsedDocument parsedDocument, CSharpAstResolver resolver, TextLocation loc, CancellationToken cancellationToken = default (CancellationToken)) : base (resolver, cancellationToken)
 
177
                {
 
178
                        this.TextEditor = data;
 
179
                        this.ParsedDocument = parsedDocument;
 
180
                        this.Project = project;
 
181
                        var policy = Project.Policies.Get<CSharpFormattingPolicy> ();
 
182
                        this.formattingOptions = policy.CreateOptions ();
 
183
                        this.location = loc;
 
184
                        var namingPolicy = Project.Policies.Get<NameConventionPolicy> ();
 
185
                        Services.AddService (typeof(NamingConventionService), namingPolicy.CreateNRefactoryService ());
 
186
                }
182
187
        }
183
188
}