Browse Source

Fix #450: FormatItem completion and partial completion

pull/463/head
Daniel Grunwald 11 years ago
parent
commit
5c3783f8e8
  1. 4
      src/AddIns/BackendBindings/CSharpBinding/Project/CSharpBinding.csproj
  2. 4
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/CSharpCompletionDataFactory.cs
  3. 14
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/CompletionData.cs
  4. 53
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/FormatItemCompletionData.cs
  5. 93
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/PartialCompletionData.cs

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

@ -79,17 +79,19 @@ @@ -79,17 +79,19 @@
<Compile Include="Src\Completion\CSharpInsightItem.cs" />
<Compile Include="Src\Completion\CSharpMethodInsight.cs" />
<Compile Include="Src\Completion\EventCreationCompletionData.cs" />
<Compile Include="Src\Completion\FormatItemCompletionData.cs" />
<Compile Include="Src\Completion\ImportCompletionData.cs" />
<Compile Include="Src\Completion\OverrideCompletionData.cs" />
<Compile Include="Src\Completion\OverrideEqualsGetHashCodeCompletionData.cs" />
<Compile Include="Src\Completion\OverrideToStringCompletionData.cs" />
<Compile Include="Src\Completion\PartialCompletionData.cs" />
<Compile Include="Src\Completion\SegmentTrackingOutputFormatter.cs" />
<Compile Include="Src\Completion\TypeCompletionData.cs" />
<Compile Include="Src\Completion\XmlDocCompletionData.cs" />
<Compile Include="Src\CSharpSemanticHighlighterVisitor.cs">
<DependentUpon>CSharpSemanticHighlighter.cs</DependentUpon>
</Compile>
<Compile Include="Src\FormattingStrategy\CSharpFormattingOptionsContainer.cs" />
<Compile Include="Src\FormattingStrategy\CSharpFormattingOptionsContainer.cs" />
<Compile Include="Src\FormattingStrategy\CSharpFormatter.cs" />
<Compile Include="Src\FormattingStrategy\CSharpFormattingOptionsPersistence.cs" />
<Compile Include="Src\FormattingStrategy\FormattingOptionBinding.cs" />

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

@ -136,7 +136,7 @@ namespace CSharpBinding.Completion @@ -136,7 +136,7 @@ namespace CSharpBinding.Completion
ICompletionData ICompletionDataFactory.CreateNewPartialCompletionData(int declarationBegin, IUnresolvedTypeDefinition type, IUnresolvedMember m)
{
return new CompletionData("TODO: partial completion");
return new PartialCompletionData(declarationBegin, m.Resolve(contextAtCaret.CurrentTypeResolveContext), contextAtCaret);
}
IEnumerable<ICompletionData> ICompletionDataFactory.CreateCodeTemplateCompletionData()
@ -162,7 +162,7 @@ namespace CSharpBinding.Completion @@ -162,7 +162,7 @@ namespace CSharpBinding.Completion
ICompletionData ICompletionDataFactory.CreateFormatItemCompletionData(string format, string description, object example)
{
return new CompletionData("TODO: format item completion");
return new FormatItemCompletionData(format, description, example);
}
ICompletionData ICompletionDataFactory.CreateXmlDocCompletionData(string tag, string description, string tagInsertionText)

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

@ -87,8 +87,20 @@ namespace CSharpBinding.Completion @@ -87,8 +87,20 @@ namespace CSharpBinding.Completion
context.EndOffset = context.StartOffset + this.CompletionText.Length;
}
object fancyContent;
object IFancyCompletionItem.Content {
get { return this.DisplayText; }
get {
if (fancyContent == null) {
fancyContent = CreateFancyContent();
}
return fancyContent;
}
}
protected virtual object CreateFancyContent()
{
return DisplayText;
}
object fancyDescription;

53
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/FormatItemCompletionData.cs

@ -0,0 +1,53 @@ @@ -0,0 +1,53 @@
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using ICSharpCode.SharpDevelop.Editor.CodeCompletion;
namespace CSharpBinding.Completion
{
class FormatItemCompletionData : CompletionData
{
readonly string description;
readonly string format;
public FormatItemCompletionData(string format, string description, object example)
: base(format)
{
this.description = description;
this.format = format;
this.DisplayText = format + " - " + description;
try {
this.Description = string.Format("{0:" + format + "}", example);
} catch (FormatException) {
}
}
protected override object CreateFancyContent()
{
TextBlock textBlock = new TextBlock();
textBlock.Inlines.Add(new Run(format));
textBlock.Inlines.Add(new Run(" - " + description) { Foreground = SystemColors.GrayTextBrush });
return textBlock;
}
}
}

93
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/PartialCompletionData.cs

@ -0,0 +1,93 @@ @@ -0,0 +1,93 @@
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.CSharp.Refactoring;
using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Editor.CodeCompletion;
using CSharpBinding.FormattingStrategy;
namespace CSharpBinding.Completion
{
/// <summary>
/// Item for 'partial' completion.
/// </summary>
class PartialCompletionData : EntityCompletionData
{
protected readonly int declarationBegin;
protected readonly CSharpResolver contextAtCaret;
public PartialCompletionData(int declarationBegin, IMember m, CSharpResolver contextAtCaret)
: base(m)
{
this.declarationBegin = declarationBegin;
this.contextAtCaret = contextAtCaret;
var ambience = new CSharpAmbience();
ambience.ConversionFlags = ConversionFlags.ShowTypeParameterList | ConversionFlags.ShowParameterList | ConversionFlags.ShowParameterNames;
this.CompletionText = ambience.ConvertSymbol(m);
}
public override void Complete(CompletionContext context)
{
if (declarationBegin > context.StartOffset) {
base.Complete(context);
return;
}
TypeSystemAstBuilder b = new TypeSystemAstBuilder(contextAtCaret);
b.GenerateBody = true;
var entityDeclaration = b.ConvertEntity(this.Entity);
entityDeclaration.Modifiers &= ~Modifiers.VisibilityMask; // remove visiblity
entityDeclaration.Modifiers |= Modifiers.Partial;
var document = context.Editor.Document;
StringWriter w = new StringWriter();
var formattingOptions = CSharpFormattingOptionsPersistence.GetProjectOptions(contextAtCaret.Compilation.GetProject());
var segmentDict = SegmentTrackingOutputFormatter.WriteNode(
w, entityDeclaration, formattingOptions.OptionsContainer.GetEffectiveOptions(), context.Editor.Options);
using (document.OpenUndoGroup()) {
string newText = w.ToString().TrimEnd();
document.Replace(declarationBegin, context.EndOffset - declarationBegin, newText);
var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);
if (throwStatement != null) {
var segment = segmentDict[throwStatement];
context.Editor.Select(declarationBegin + segment.Offset, segment.Length);
}
CSharpFormatterHelper.Format(context.Editor, declarationBegin, newText.Length, formattingOptions.OptionsContainer);
}
}
IEnumerable<Expression> ParametersToExpressions(IEntity entity)
{
foreach (var p in ((IParameterizedMember)entity).Parameters) {
if (p.IsRef || p.IsOut)
yield return new DirectionExpression(p.IsOut ? FieldDirection.Out : FieldDirection.Ref, new IdentifierExpression(p.Name));
else
yield return new IdentifierExpression(p.Name);
}
}
}
}
Loading…
Cancel
Save