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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Mono.Debugger.Frontend
{
public class Completer {
private Engine engine;
public Completer (Engine engine) {
this.engine = engine;
}
/* This method gets installed as the GnuReadLine completion
* delegate. It completes commands at the start of the
* line, and does command specific completion for
* arguments. */
public void CompletionHandler (string text, int start, int end) {
if (start == 0) {
CommandCompleter (text, start, end);
}
else {
/* we look up the command in the
* current line buffer and call that
* command's completion generator to
* generate the list of strings.
*/
string line = GnuReadLine.CurrentLine;
string command;
int ptr = 0;
while (!Char.IsWhiteSpace (line[ptr]))
++ptr;
command = line.Substring (0, ptr);
Command c = engine.Get (command, null);
if (c != null) {
c.Complete (engine, text, start, end);
}
}
}
string ComputeMCP (ArrayList choices, string initial_prefix)
{
string s = (string)choices[0];
int maxlen = s.Length;
for (int i = 1; i < choices.Count; i ++) {
if (maxlen > ((string)choices[i]).Length)
maxlen = ((string)choices[i]).Length;
}
s = s.Substring (0, maxlen);
for (int l = initial_prefix.Length; l < maxlen; l ++) {
for (int i = 1; i < choices.Count; i ++) {
string test = (string)choices[i];
if (test[l] != s[l])
return s.Substring (0, l);
}
}
return s;
}
/*
* some prebuilt completers, for use by commands.
*/
/* CommandCompleter: completes against the list of commands
* (but not aliases) that have been registered with the
* engine. This is used by the CompletionHandler itself to
* complete commands at the beginning of the line. */
public void CommandCompleter (string text, int start, int end)
{
/* complete possible commands */
ArrayList matched_commands = new ArrayList();
string[] match_strings = null;
foreach (string key in engine.Commands.Keys) {
if (key.StartsWith (text))
matched_commands.Add (key);
}
if (matched_commands.Count > 0) {
if (matched_commands.Count > 1) {
/* always add the prefix at
* the beginning when we have
* > 1 matches, so that
* readline will display the
* matches. */
matched_commands.Insert (0, ComputeMCP (matched_commands, text));
}
match_strings = new string [matched_commands.Count + 1];
matched_commands.CopyTo (match_strings);
match_strings [matched_commands.Count] = null;
}
GnuReadLine.SetCompletionMatches (match_strings);
}
public void StringsCompleter (string[] haystack, string text, int start, int end)
{
ArrayList matches = new ArrayList();
foreach (string s in haystack) {
if (s.ToLower().StartsWith (text))
matches.Add (s.ToLower());
}
string[] match_strings = null;
if (matches.Count > 0) {
if (matches.Count > 1) {
/* always add the prefix at
* the beginning when we have
* > 1 matches, so that
* readline will display the
* matches. */
matches.Insert (0, ComputeMCP (matches, text));
}
match_strings = new string [matches.Count + 1];
matches.CopyTo (match_strings);
match_strings [matches.Count] = null;
}
GnuReadLine.SetCompletionMatches (match_strings);
}
public void ArgumentCompleter (Type t, string text, int start, int end)
{
ArrayList matched_args = new ArrayList();
PropertyInfo [] pi = t.GetProperties ();
foreach (PropertyInfo p in pi) {
if (!p.CanWrite)
continue;
if (text == "-" ||
p.Name.ToLower().StartsWith (text.Substring (1))) {
matched_args.Add ("-" + p.Name.ToLower());
}
}
string[] match_strings = null;
if (matched_args.Count > 0) {
if (matched_args.Count > 1) {
/* always add the prefix at
* the beginning when we have
* > 1 matches, so that
* readline will display the
* matches. */
matched_args.Insert (0, ComputeMCP (matched_args, text));
}
match_strings = new string [matched_args.Count + 1];
matched_args.CopyTo (match_strings);
match_strings [matched_args.Count] = null;
}
GnuReadLine.SetCompletionMatches (match_strings);
}
public void FilenameCompleter (string text, int start, int end)
{
string dir;
string file_prefix;
DebuggerEngine de = engine as DebuggerEngine;
GnuReadLine.FilenameCompletionDesired = true;
if (text.IndexOf (Path.DirectorySeparatorChar) == -1) {
dir = de.Interpreter.Options.WorkingDirectory;
file_prefix = text;
}
else {
dir = text.Substring (0, text.LastIndexOf (Path.DirectorySeparatorChar) + 1);
file_prefix = text.Substring (text.LastIndexOf (Path.DirectorySeparatorChar) + 1);
}
string[] fs_entries;
try {
fs_entries = Directory.GetFileSystemEntries (dir, file_prefix + "*");
} catch {
GnuReadLine.SetCompletionMatches (null);
return;
}
ArrayList matched_paths = new ArrayList();
foreach (string f in fs_entries) {
if (f.StartsWith (dir + file_prefix)) {
matched_paths.Add (f);
}
}
string[] match_strings = null;
if (matched_paths.Count > 0) {
if (matched_paths.Count > 1) {
/* always add the prefix at
* the beginning when we have
* > 1 matches, so that
* readline will display the
* matches. */
matched_paths.Insert (0, ComputeMCP (matched_paths, text));
}
match_strings = new string [matched_paths.Count + 1];
matched_paths.CopyTo (match_strings);
match_strings [matched_paths.Count] = null;
}
GnuReadLine.SetCompletionMatches (match_strings);
}
/* NoopCompleter: always returns an empty list (no
* matches). */
public void NoopCompleter (string text, int start, int end)
{
GnuReadLine.SetCompletionMatches (null);
}
public void SymbolCompleter (ScriptingContext context, string text, int start, int end)
{
try {
ArrayList method_list = new ArrayList ();
string[] namespaces = context.GetNamespaces();
Module[] modules = context.CurrentProcess.Modules;
foreach (Module module in modules) {
if (!module.SymbolsLoaded || !module.SymbolTable.HasMethods)
continue;
SourceFile[] sources = module.Sources;
if (sources == null)
continue;
foreach (SourceFile sf in sources) {
foreach (MethodSource method in sf.Methods) {
if (method.Name.StartsWith (text)) {
int parameter_start = method.Name.IndexOf ('(');
if (parameter_start != -1)
method_list.Add (method.Name.Substring (0, parameter_start));
else
method_list.Add (method.Name);
}
if (namespaces != null) {
foreach (string n in namespaces) {
if (n != "" && method.Name.StartsWith (String.Concat (n, ".", text))) {
int parameter_start = method.Name.IndexOf ('(');
if (parameter_start != -1)
method_list.Add (method.Name.Substring (n.Length + 1,
parameter_start - n.Length - 1));
else
method_list.Add (method.Name.Substring (n.Length + 1));
}
}
}
}
}
}
string[] methods = null;
if (method_list.Count > 0) {
method_list.Insert (0, ComputeMCP (method_list, text));
methods = new string [method_list.Count + 1];
method_list.CopyTo (methods);
methods [method_list.Count] = null;
}
GnuReadLine.SetCompletionMatches (methods);
} catch {
GnuReadLine.SetCompletionMatches (null);
}
}
}
}
|