This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
/
ConsolePad.cs
153 lines (126 loc) · 4.18 KB
/
ConsolePad.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the BSD license (for details please see \src\AddIns\Debugger\Debugger.AddIn\license.txt)
using System;
using System.Windows.Controls;
using System.Windows.Input;
using Debugger;
using ICSharpCode.Core.Presentation;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Visitors;
using ICSharpCode.SharpDevelop.Debugging;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver;
using ICSharpCode.SharpDevelop.Editor.CodeCompletion;
using ICSharpCode.SharpDevelop.Services;
namespace ICSharpCode.SharpDevelop.Gui.Pads
{
public class ConsolePad : AbstractConsolePad
{
SupportedLanguage language;
NRefactoryResolver resolver;
const string debuggerConsoleToolBarTreePath = "/SharpDevelop/Pads/ConsolePad/ToolBar";
public SupportedLanguage SelectedLanguage {
get { return language; }
set {
this.language = value;
OnLanguageChanged();
}
}
protected override bool AcceptCommand(string command)
{
if (!string.IsNullOrEmpty(command)) {
string result = Evaluate(command);
if (!string.IsNullOrEmpty(result)) {
Append(Environment.NewLine + result);
}
}
return true;
}
string Evaluate(string code)
{
if (process == null) {
return "No process is being debugged";
}
if (process.IsRunning) {
return "The process is running";
}
try {
var context = !process.IsInExternalCode ? process.SelectedStackFrame : process.SelectedThread.MostRecentStackFrame;
Value val = ExpressionEvaluator.Evaluate(code, SelectedLanguage, context);
return ExpressionEvaluator.FormatValue(val);
} catch (GetValueException e) {
return e.Message;
}
}
Process process;
public Process Process {
get { return process; }
set { process = value; }
}
protected override string Prompt {
get {
return "> ";
}
}
protected override void InitializeConsole()
{
base.InitializeConsole();
OnLanguageChanged();
}
void OnLanguageChanged()
{
switch (SelectedLanguage) {
case SupportedLanguage.CSharp:
resolver = new NRefactoryResolver(LanguageProperties.CSharp);
SetHighlighting("C#");
break;
case SupportedLanguage.VBNet:
resolver = new NRefactoryResolver(LanguageProperties.VBNet);
SetHighlighting("VBNET");
break;
}
}
public ConsolePad()
{
WindowsDebugger debugger = (WindowsDebugger)DebuggerService.CurrentDebugger;
debugger.ProcessSelected += delegate(object sender, ProcessEventArgs e) {
this.Process = e.Process;
};
this.Process = debugger.DebuggedProcess;
}
protected override void AbstractConsolePadTextEntered(object sender, TextCompositionEventArgs e)
{
if (this.process == null || this.process.IsRunning)
return;
var context = !process.IsInExternalCode ? process.SelectedStackFrame : process.SelectedThread.MostRecentStackFrame;
if (context == null)
return;
foreach (char ch in e.Text) {
if (ch == '.') {
ShowDotCompletion(console.CommandText);
}
}
}
void ShowDotCompletion(string currentText)
{
var context = !process.IsInExternalCode ? process.SelectedStackFrame : process.SelectedThread.MostRecentStackFrame;
if (context == null)
return;
var seg = context.NextStatement;
var expressionFinder = ParserService.GetExpressionFinder(seg.Filename);
var info = ParserService.GetParseInformation(seg.Filename);
string text = ParserService.GetParseableFileContent(seg.Filename).Text;
int currentOffset = TextEditor.Caret.Offset - console.CommandOffset - 1;
var expr = expressionFinder.FindExpression(currentText, currentOffset);
expr.Region = new DomRegion(seg.StartLine, seg.StartColumn, seg.EndLine, seg.EndColumn);
var rr = resolver.Resolve(expr, info, text);
if (rr != null) {
TextEditor.ShowCompletionWindow(new DotCodeCompletionItemProvider().GenerateCompletionListForResolveResult(rr, expr.Context));
}
}
protected override ToolBar BuildToolBar()
{
return ToolBarService.CreateToolBar(console, this, debuggerConsoleToolBarTreePath);
}
}
}