|
| 1 | +// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 4 | +// software and associated documentation files (the "Software"), to deal in the Software |
| 5 | +// without restriction, including without limitation the rights to use, copy, modify, merge, |
| 6 | +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
| 7 | +// to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | +// |
| 9 | +// The above copyright notice and this permission notice shall be included in all copies or |
| 10 | +// substantial portions of the Software. |
| 11 | +// |
| 12 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 13 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 14 | +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 15 | +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 16 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 17 | +// DEALINGS IN THE SOFTWARE. |
| 18 | + |
| 19 | +using System; |
| 20 | +using System.Collections.Generic; |
| 21 | +using System.IO; |
| 22 | +using System.Linq; |
| 23 | +using ICSharpCode.NRefactory.CSharp; |
| 24 | +using ICSharpCode.NRefactory.CSharp.Refactoring; |
| 25 | +using ICSharpCode.NRefactory.CSharp.Resolver; |
| 26 | +using ICSharpCode.NRefactory.TypeSystem; |
| 27 | +using ICSharpCode.SharpDevelop; |
| 28 | +using ICSharpCode.SharpDevelop.Editor.CodeCompletion; |
| 29 | +using CSharpBinding.FormattingStrategy; |
| 30 | + |
| 31 | +namespace CSharpBinding.Completion |
| 32 | +{ |
| 33 | + /// <summary> |
| 34 | + /// Item for 'partial' completion. |
| 35 | + /// </summary> |
| 36 | + class PartialCompletionData : EntityCompletionData |
| 37 | + { |
| 38 | + protected readonly int declarationBegin; |
| 39 | + protected readonly CSharpResolver contextAtCaret; |
| 40 | + |
| 41 | + public PartialCompletionData(int declarationBegin, IMember m, CSharpResolver contextAtCaret) |
| 42 | + : base(m) |
| 43 | + { |
| 44 | + this.declarationBegin = declarationBegin; |
| 45 | + this.contextAtCaret = contextAtCaret; |
| 46 | + var ambience = new CSharpAmbience(); |
| 47 | + ambience.ConversionFlags = ConversionFlags.ShowTypeParameterList | ConversionFlags.ShowParameterList | ConversionFlags.ShowParameterNames; |
| 48 | + this.CompletionText = ambience.ConvertSymbol(m); |
| 49 | + } |
| 50 | + |
| 51 | + public override void Complete(CompletionContext context) |
| 52 | + { |
| 53 | + if (declarationBegin > context.StartOffset) { |
| 54 | + base.Complete(context); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + TypeSystemAstBuilder b = new TypeSystemAstBuilder(contextAtCaret); |
| 59 | + b.GenerateBody = true; |
| 60 | + |
| 61 | + var entityDeclaration = b.ConvertEntity(this.Entity); |
| 62 | + entityDeclaration.Modifiers &= ~Modifiers.VisibilityMask; // remove visiblity |
| 63 | + entityDeclaration.Modifiers |= Modifiers.Partial; |
| 64 | + |
| 65 | + var document = context.Editor.Document; |
| 66 | + StringWriter w = new StringWriter(); |
| 67 | + var formattingOptions = CSharpFormattingOptionsPersistence.GetProjectOptions(contextAtCaret.Compilation.GetProject()); |
| 68 | + var segmentDict = SegmentTrackingOutputFormatter.WriteNode( |
| 69 | + w, entityDeclaration, formattingOptions.OptionsContainer.GetEffectiveOptions(), context.Editor.Options); |
| 70 | + |
| 71 | + using (document.OpenUndoGroup()) { |
| 72 | + string newText = w.ToString().TrimEnd(); |
| 73 | + document.Replace(declarationBegin, context.EndOffset - declarationBegin, newText); |
| 74 | + var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement); |
| 75 | + if (throwStatement != null) { |
| 76 | + var segment = segmentDict[throwStatement]; |
| 77 | + context.Editor.Select(declarationBegin + segment.Offset, segment.Length); |
| 78 | + } |
| 79 | + CSharpFormatterHelper.Format(context.Editor, declarationBegin, newText.Length, formattingOptions.OptionsContainer); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + IEnumerable<Expression> ParametersToExpressions(IEntity entity) |
| 84 | + { |
| 85 | + foreach (var p in ((IParameterizedMember)entity).Parameters) { |
| 86 | + if (p.IsRef || p.IsOut) |
| 87 | + yield return new DirectionExpression(p.IsOut ? FieldDirection.Out : FieldDirection.Ref, new IdentifierExpression(p.Name)); |
| 88 | + else |
| 89 | + yield return new IdentifierExpression(p.Name); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments