1 | #region License Information
|
---|
2 | // CShell, A Simple C# Scripting IDE
|
---|
3 | // Copyright (C) 2013 Arnova Asset Management Ltd., Lukas Buhler
|
---|
4 | //
|
---|
5 | // This program is free software: you can redistribute it and/or modify
|
---|
6 | // it under the terms of the GNU General Public License as published by
|
---|
7 | // the Free Software Foundation, either version 3 of the License, or
|
---|
8 | // (at your option) any later version.
|
---|
9 | //
|
---|
10 | // This program is distributed in the hope that it will be useful,
|
---|
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | // GNU General Public License for more details.
|
---|
14 | //
|
---|
15 | // You should have received a copy of the GNU General Public License
|
---|
16 | // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 |
|
---|
18 | // This file is based on code from the SharpDevelop project:
|
---|
19 | // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \Doc\sharpdevelop-copyright.txt)
|
---|
20 | // This code is distributed under the GNU LGPL (for details please see \Doc\COPYING.LESSER.txt)
|
---|
21 |
|
---|
22 | /* HeuristicLab
|
---|
23 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
24 | *
|
---|
25 | * This file is part of HeuristicLab.
|
---|
26 | *
|
---|
27 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
28 | * it under the terms of the GNU General Public License as published by
|
---|
29 | * the Free Software Foundation, either version 3 of the License, or
|
---|
30 | * (at your option) any later version.
|
---|
31 | *
|
---|
32 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
33 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
34 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
35 | * GNU General Public License for more details.
|
---|
36 | *
|
---|
37 | * You should have received a copy of the GNU General Public License
|
---|
38 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
39 | */
|
---|
40 | #endregion
|
---|
41 |
|
---|
42 | using System.IO;
|
---|
43 | using System.Text;
|
---|
44 | using System.Windows;
|
---|
45 | using System.Windows.Controls;
|
---|
46 | using ICSharpCode.AvalonEdit.Highlighting;
|
---|
47 | using ICSharpCode.NRefactory.CSharp;
|
---|
48 | using ICSharpCode.NRefactory.Editor;
|
---|
49 | using ICSharpCode.NRefactory.TypeSystem;
|
---|
50 |
|
---|
51 | namespace HeuristicLab.CodeEditor {
|
---|
52 | internal sealed class CSharpInsightItem {
|
---|
53 | public readonly IParameterizedMember Method;
|
---|
54 |
|
---|
55 | public CSharpInsightItem(IParameterizedMember method) {
|
---|
56 | this.Method = method;
|
---|
57 | }
|
---|
58 |
|
---|
59 | TextBlock header;
|
---|
60 |
|
---|
61 | public object Header {
|
---|
62 | get {
|
---|
63 | if (header == null) {
|
---|
64 | header = new TextBlock();
|
---|
65 | GenerateHeader();
|
---|
66 | }
|
---|
67 | return header;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | int highlightedParameterIndex = -1;
|
---|
72 |
|
---|
73 | public void HighlightParameter(int parameterIndex) {
|
---|
74 | if (highlightedParameterIndex == parameterIndex)
|
---|
75 | return;
|
---|
76 | this.highlightedParameterIndex = parameterIndex;
|
---|
77 | if (header != null)
|
---|
78 | GenerateHeader();
|
---|
79 | }
|
---|
80 |
|
---|
81 | void GenerateHeader() {
|
---|
82 | var ambience = new CSharpAmbience { ConversionFlags = ConversionFlags.StandardConversionFlags };
|
---|
83 | var stringBuilder = new StringBuilder();
|
---|
84 | var formatter = new ParameterHighlightingOutputFormatter(stringBuilder, highlightedParameterIndex);
|
---|
85 | ambience.ConvertSymbol(Method, formatter, FormattingOptionsFactory.CreateSharpDevelop());
|
---|
86 | var textSource = new StringTextSource(stringBuilder.ToString());
|
---|
87 | var richTextModel = new RichTextModel();
|
---|
88 | richTextModel.SetFontWeight(formatter.parameterStartOffset, formatter.parameterLength, FontWeights.Bold);
|
---|
89 | header.Inlines.Clear();
|
---|
90 | header.Inlines.AddRange(richTextModel.CreateRuns(textSource));
|
---|
91 | }
|
---|
92 |
|
---|
93 | public object Content {
|
---|
94 | get { return Documentation; }
|
---|
95 | }
|
---|
96 |
|
---|
97 | private string documentation;
|
---|
98 | public string Documentation {
|
---|
99 | get {
|
---|
100 | if (documentation == null) {
|
---|
101 | if (Method.Documentation == null)
|
---|
102 | documentation = string.Empty;
|
---|
103 | else
|
---|
104 | documentation = EntityCompletionData.XmlDocumentationToText(Method.Documentation);
|
---|
105 | }
|
---|
106 | return documentation;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | sealed class ParameterHighlightingOutputFormatter : TextWriterTokenWriter {
|
---|
111 | StringBuilder b;
|
---|
112 | int highlightedParameterIndex;
|
---|
113 | int parameterIndex;
|
---|
114 | internal int parameterStartOffset;
|
---|
115 | internal int parameterLength;
|
---|
116 |
|
---|
117 | public ParameterHighlightingOutputFormatter(StringBuilder b, int highlightedParameterIndex)
|
---|
118 | : base(new StringWriter(b)) {
|
---|
119 | this.b = b;
|
---|
120 | this.highlightedParameterIndex = highlightedParameterIndex;
|
---|
121 | }
|
---|
122 |
|
---|
123 | public override void StartNode(AstNode node) {
|
---|
124 | if (parameterIndex == highlightedParameterIndex && node is ParameterDeclaration) {
|
---|
125 | parameterStartOffset = b.Length;
|
---|
126 | }
|
---|
127 | base.StartNode(node);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public override void EndNode(AstNode node) {
|
---|
131 | base.EndNode(node);
|
---|
132 | if (node is ParameterDeclaration) {
|
---|
133 | if (parameterIndex == highlightedParameterIndex)
|
---|
134 | parameterLength = b.Length - parameterStartOffset;
|
---|
135 | parameterIndex++;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|