20 changed files with 127 additions and 39 deletions
@ -0,0 +1,54 @@
@@ -0,0 +1,54 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||
//
|
||||
using System; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
|
||||
using CSharpBinding.Refactoring; |
||||
using ICSharpCode.NRefactory.CSharp; |
||||
using ICSharpCode.NRefactory.CSharp.Refactoring; |
||||
using ICSharpCode.NRefactory.CSharp.Resolver; |
||||
using ICSharpCode.NRefactory.CSharp.TypeSystem; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
using ICSharpCode.SharpDevelop; |
||||
using ICSharpCode.SharpDevelop.Editor.CodeCompletion; |
||||
|
||||
namespace CSharpBinding.Completion |
||||
{ |
||||
class ImportCompletionData : EntityCompletionData |
||||
{ |
||||
string insertUsing; |
||||
string insertionText; |
||||
|
||||
public ImportCompletionData(ITypeDefinition typeDef, CSharpTypeResolveContext contextAtCaret, bool useFullName) |
||||
: base(typeDef) |
||||
{ |
||||
this.Description = "using " + typeDef.Namespace + ";"; |
||||
if (useFullName) { |
||||
var astBuilder = new TypeSystemAstBuilder(new CSharpResolver(contextAtCaret)); |
||||
insertionText = astBuilder.ConvertType(typeDef).GetText(); |
||||
} else { |
||||
insertionText = typeDef.Name; |
||||
insertUsing = typeDef.Namespace; |
||||
} |
||||
} |
||||
|
||||
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()) { |
||||
// TODO: sort usings; insert inside namespace if other usings are there; etc.
|
||||
var prevUsing = refactoringContext.RootNode.Children.LastOrDefault(p => p is UsingDeclaration); |
||||
if (prevUsing != null) { |
||||
script.InsertAfter(prevUsing, new UsingDeclaration(insertUsing)); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue