Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2922-DataCompletenessChartPerf/HeuristicLab.CodeEditor/3.4/LanguageFeatures/CodeCompletion/CSharp/CSharpInsightItem.cs @ 18242

Last change on this file since 18242 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.8 KB
Line 
1#region License Information
2// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy of this
5// software and associated documentation files (the "Software"), to deal in the Software
6// without restriction, including without limitation the rights to use, copy, modify, merge,
7// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
8// to whom the Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all copies or
11// substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
15// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
16// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18// DEALINGS IN THE SOFTWARE.
19
20/* HeuristicLab
21 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
22 *
23 * This file is part of HeuristicLab.
24 *
25 * HeuristicLab is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
29 *
30 * HeuristicLab is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
37 */
38#endregion
39
40using System.IO;
41using System.Text;
42using System.Windows;
43using System.Windows.Controls;
44using ICSharpCode.NRefactory.CSharp;
45using ICSharpCode.NRefactory.TypeSystem;
46using ICSharpCode.NRefactory.Xml;
47
48namespace HeuristicLab.CodeEditor {
49  sealed class CSharpInsightItem {
50    public readonly IParameterizedMember Method;
51
52    public CSharpInsightItem(IParameterizedMember method) {
53      this.Method = method;
54    }
55
56    FlowDocumentScrollViewer header;
57
58    public object Header {
59      get {
60        if (header == null) {
61          header = GenerateHeader();
62        }
63        return header;
64      }
65    }
66
67    int highlightedParameterIndex = -1;
68
69    public void HighlightParameter(int parameterIndex) {
70      if (highlightedParameterIndex == parameterIndex)
71        return;
72      this.highlightedParameterIndex = parameterIndex;
73      if (header != null)
74        header = GenerateHeader();
75    }
76
77    FlowDocumentScrollViewer GenerateHeader() {
78      var ambience = new CSharpAmbience();
79      ambience.ConversionFlags = ConversionFlags.StandardConversionFlags;
80      var stringBuilder = new StringBuilder();
81      var formatter = new ParameterHighlightingOutputFormatter(stringBuilder, highlightedParameterIndex);
82      ambience.ConvertSymbol(Method, formatter, FormattingOptionsFactory.CreateSharpDevelop());
83
84      var documentation = XmlDocumentationElement.Get(Method);
85      ambience.ConversionFlags = ConversionFlags.ShowTypeParameterList;
86
87      var b = new CSharpDocumentationBuilder(ambience);
88      string parameterName = null;
89      if (Method.Parameters.Count > highlightedParameterIndex)
90        parameterName = Method.Parameters[highlightedParameterIndex].Name;
91      b.AddSignatureBlock(stringBuilder.ToString(), formatter.parameterStartOffset, formatter.parameterLength, parameterName);
92
93      var b2 = new CSharpDocumentationBuilder(ambience);
94      b2.ParameterName = parameterName;
95      b2.ShowAllParameters = false;
96
97      if (documentation != null) {
98        foreach (var child in documentation.Children) {
99          b2.AddDocumentationElement(child);
100        }
101      }
102
103      content = new FlowDocumentScrollViewer {
104        Document = b2.CreateFlowDocument(),
105        VerticalScrollBarVisibility = ScrollBarVisibility.Auto
106      };
107
108      var flowDocument = b.CreateFlowDocument();
109      flowDocument.PagePadding = new Thickness(0); // default is NOT Thickness(0), but Thickness(Auto), which adds unnecessary space
110
111      return new FlowDocumentScrollViewer {
112        Document = flowDocument,
113        VerticalScrollBarVisibility = ScrollBarVisibility.Auto
114      };
115    }
116
117    FlowDocumentScrollViewer content;
118
119    public object Content {
120      get {
121        if (content == null) {
122          GenerateHeader();
123        }
124        return content;
125      }
126    }
127
128    sealed class ParameterHighlightingOutputFormatter : TextWriterTokenWriter {
129      StringBuilder b;
130      int highlightedParameterIndex;
131      int parameterIndex;
132      internal int parameterStartOffset;
133      internal int parameterLength;
134
135      public ParameterHighlightingOutputFormatter(StringBuilder b, int highlightedParameterIndex)
136        : base(new StringWriter(b)) {
137        this.b = b;
138        this.highlightedParameterIndex = highlightedParameterIndex;
139      }
140
141      public override void StartNode(AstNode node) {
142        if (parameterIndex == highlightedParameterIndex && node is ParameterDeclaration) {
143          parameterStartOffset = b.Length;
144        }
145        base.StartNode(node);
146      }
147
148      public override void EndNode(AstNode node) {
149        base.EndNode(node);
150        if (node is ParameterDeclaration) {
151          if (parameterIndex == highlightedParameterIndex)
152            parameterLength = b.Length - parameterStartOffset;
153          parameterIndex++;
154        }
155      }
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.