Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 5c3783f

Browse files
committed
Fix #450: FormatItem completion and partial completion
1 parent c11c509 commit 5c3783f

5 files changed

Lines changed: 164 additions & 4 deletions

File tree

src/AddIns/BackendBindings/CSharpBinding/Project/CSharpBinding.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,19 @@
7979
<Compile Include="Src\Completion\CSharpInsightItem.cs" />
8080
<Compile Include="Src\Completion\CSharpMethodInsight.cs" />
8181
<Compile Include="Src\Completion\EventCreationCompletionData.cs" />
82+
<Compile Include="Src\Completion\FormatItemCompletionData.cs" />
8283
<Compile Include="Src\Completion\ImportCompletionData.cs" />
8384
<Compile Include="Src\Completion\OverrideCompletionData.cs" />
8485
<Compile Include="Src\Completion\OverrideEqualsGetHashCodeCompletionData.cs" />
8586
<Compile Include="Src\Completion\OverrideToStringCompletionData.cs" />
87+
<Compile Include="Src\Completion\PartialCompletionData.cs" />
8688
<Compile Include="Src\Completion\SegmentTrackingOutputFormatter.cs" />
8789
<Compile Include="Src\Completion\TypeCompletionData.cs" />
8890
<Compile Include="Src\Completion\XmlDocCompletionData.cs" />
8991
<Compile Include="Src\CSharpSemanticHighlighterVisitor.cs">
9092
<DependentUpon>CSharpSemanticHighlighter.cs</DependentUpon>
9193
</Compile>
92-
<Compile Include="Src\FormattingStrategy\CSharpFormattingOptionsContainer.cs" />
94+
<Compile Include="Src\FormattingStrategy\CSharpFormattingOptionsContainer.cs" />
9395
<Compile Include="Src\FormattingStrategy\CSharpFormatter.cs" />
9496
<Compile Include="Src\FormattingStrategy\CSharpFormattingOptionsPersistence.cs" />
9597
<Compile Include="Src\FormattingStrategy\FormattingOptionBinding.cs" />

src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/CSharpCompletionDataFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ ICompletionData ICompletionDataFactory.CreateNewOverrideCompletionData(int decla
136136

137137
ICompletionData ICompletionDataFactory.CreateNewPartialCompletionData(int declarationBegin, IUnresolvedTypeDefinition type, IUnresolvedMember m)
138138
{
139-
return new CompletionData("TODO: partial completion");
139+
return new PartialCompletionData(declarationBegin, m.Resolve(contextAtCaret.CurrentTypeResolveContext), contextAtCaret);
140140
}
141141

142142
IEnumerable<ICompletionData> ICompletionDataFactory.CreateCodeTemplateCompletionData()
@@ -162,7 +162,7 @@ ICompletionData ICompletionDataFactory.CreateImportCompletionData(IType type, bo
162162

163163
ICompletionData ICompletionDataFactory.CreateFormatItemCompletionData(string format, string description, object example)
164164
{
165-
return new CompletionData("TODO: format item completion");
165+
return new FormatItemCompletionData(format, description, example);
166166
}
167167

168168
ICompletionData ICompletionDataFactory.CreateXmlDocCompletionData(string tag, string description, string tagInsertionText)

src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/CompletionData.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,20 @@ public virtual void Complete(CompletionContext context)
8787
context.EndOffset = context.StartOffset + this.CompletionText.Length;
8888
}
8989

90+
object fancyContent;
91+
9092
object IFancyCompletionItem.Content {
91-
get { return this.DisplayText; }
93+
get {
94+
if (fancyContent == null) {
95+
fancyContent = CreateFancyContent();
96+
}
97+
return fancyContent;
98+
}
99+
}
100+
101+
protected virtual object CreateFancyContent()
102+
{
103+
return DisplayText;
92104
}
93105

94106
object fancyDescription;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.Windows;
22+
using System.Windows.Controls;
23+
using System.Windows.Documents;
24+
using ICSharpCode.SharpDevelop.Editor.CodeCompletion;
25+
26+
namespace CSharpBinding.Completion
27+
{
28+
class FormatItemCompletionData : CompletionData
29+
{
30+
readonly string description;
31+
readonly string format;
32+
33+
public FormatItemCompletionData(string format, string description, object example)
34+
: base(format)
35+
{
36+
this.description = description;
37+
this.format = format;
38+
this.DisplayText = format + " - " + description;
39+
try {
40+
this.Description = string.Format("{0:" + format + "}", example);
41+
} catch (FormatException) {
42+
}
43+
}
44+
45+
protected override object CreateFancyContent()
46+
{
47+
TextBlock textBlock = new TextBlock();
48+
textBlock.Inlines.Add(new Run(format));
49+
textBlock.Inlines.Add(new Run(" - " + description) { Foreground = SystemColors.GrayTextBrush });
50+
return textBlock;
51+
}
52+
}
53+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)