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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
|
#region license
// Copyright (c) 2005, Peter Johanson (latexer@gentoo.org)
// All rights reserved.
//
// BooBinding is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// BooBinding is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with BooBinding; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#endregion
namespace BooBinding.Gui
import System
import System.Collections
import System.IO
import Gtk
import Gdk
import MonoDevelop.Components
import MonoDevelop.Ide.CodeCompletion
import MonoDevelop.Core
import MonoDevelop.Projects
import MonoDevelop.Ide
import MonoDevelop.Projects.Dom.Parser
/*
* TODO
*
* 1) Don't record lines with errors in the _scriptLines buffer
*/
class ShellTextView (TextView, ICompletionWidget):
private static _promptRegular = ">>> "
private static _promptMultiline = "... "
[Getter(Model)]
model as IShellModel
private _scriptLines = ""
private _commandHistoryPast as Stack = Stack()
private _commandHistoryFuture as Stack = Stack()
private _inBlock as bool = false
private _blockText = ""
private _reset_clears_history as bool
private _reset_clears_scrollback as bool
private _auto_indent as bool
private _load_assembly_after_build as bool
private _proj as Project
private _assembliesLoaded as bool
private _fakeProject as DotNetProject
private _fakeSolution as Solution
private _fakeFileName as string
private _fileInfo as FileStream
private _parserContext as ProjectDom;
def constructor(model as IShellModel):
self.model = model
self.WrapMode = Gtk.WrapMode.Word
self.ModifyFont(Model.Properties.Font)
# FIXME: Put the project file somewhere other than /tmp
shellProjectFile = System.IO.Path.Combine (MonoDevelop.Core.PropertyService.ConfigPath, "${Model.LanguageName}-shell-project.mdp")
// 'touch' the file so the MD parsing foo sees it as existing.
_fakeFileName = System.IO.Path.Combine (MonoDevelop.Core.PropertyService.ConfigPath, "shell-dummy-file.${Model.MimeTypeExtension}")
if not System.IO.File.Exists (_fakeFileName):
_fileInfo = System.IO.File.Create (_fakeFileName)
_fileInfo.Close ()
_fakeProject = DotNetAssemblyProject(Model.LanguageName, Name: "___ShellProject", FileName: shellProjectFile)
_fakeSolution = Solution()
_fakeSolution.RootFolder.AddItem(_fakeProject)
ProjectDomService.Load (_fakeSolution)
_parserContext = ProjectDomService.GetProjectDom (_fakeProject)
Model.Properties.InternalProperties.PropertyChanged += OnPropertyChanged
Model.RegisterOutputHandler (HandleOutput)
_auto_indent = Model.Properties.AutoIndentBlocks
_reset_clears_scrollback = Model.Properties.ResetClearsScrollback
_reset_clears_history = Model.Properties.ResetClearsHistory
_load_assembly_after_build = Model.Properties.LoadAssemblyAfterBuild
// The 'Freezer' tag is used to keep everything except
// the input line from being editable
tag = TextTag ("Freezer")
tag.Editable = false
Buffer.TagTable.Add (tag)
prompt (false)
IdeApp.ProjectOperations.EndBuild += ProjectCompiled
IdeApp.ProjectOperations.CurrentProjectChanged += ProjectChanged
// Run our model. Needs to happen for models which may spawn threads,
// processes, etc
Model.Run()
def ProjectChanged (sender, e as ProjectEventArgs):
_proj = e.Project
def ProjectCompiled (sender, args as BuildEventArgs):
if _load_assembly_after_build and args.Success:
Model.Reset()
resetGui()
loadProjectAssemblies ()
def loadProjectAssemblies():
for assembly in getProjectAssemblies ():
if (System.IO.File.Exists(assembly)):
Model.Reset()
Model.LoadAssembly (assembly)
_assembliesLoaded = true
def getProjectAssemblies():
_assemblies = []
if (_proj is not null):
assembly = _proj.GetOutputFileName(ConfigurationSelector.Default)
if not assembly.IsNull:
_assemblies.Add(assembly)
else:
projects = IdeApp.Workspace.GetAllProjects()
for entry as Project in projects:
if entry is null:
continue
assembly = entry.GetOutputFileName(ConfigurationSelector.Default)
if not assembly.IsNull:
_assemblies.Add(assembly)
return _assemblies
def HandleOutput():
GLib.Idle.Add (outputIdleProcessor)
def outputIdleProcessor() as bool:
output = Model.GetOutput()
if output is not null:
for line as string in output:
processOutput (line )
prompt (true)
for assembly in Model.References:
_fakeProject.AddReference(assembly)
GLib.Idle.Add () do:
ProjectDomService.Parse (_fakeProject, _fakeFileName, _scriptLines)
return false
override def Dispose():
Model.Dispose()
#region Overrides of the standard methods for event handling
override def OnPopulatePopup (menu as Gtk.Menu):
_copyScriptInput = ImageMenuItem (GettextCatalog.GetString ("Copy Script"))
_copyScriptInput.Activated += { Gtk.Clipboard.Get (Gdk.Atom.Intern ("PRIMARY", true)).Text = _scriptLines }
_copyScriptInput.Image = Gtk.Image (Stock.Copy, Gtk.IconSize.Menu)
_saveScriptToFile = ImageMenuItem (GettextCatalog.GetString ("Save Script As ..."))
_saveScriptToFile.Image = Gtk.Image (Stock.SaveAs, Gtk.IconSize.Menu)
_saveScriptToFile.Activated += OnSaveScript
_loadAssemblies = ImageMenuItem (GettextCatalog.GetString ("Load Project Assemblies (forces shell reset)"))
_loadAssemblies.Image = Gtk.Image (Stock.Add, Gtk.IconSize.Menu)
_loadAssemblies.Activated += def():
if Model.Reset ():
resetGui ()
loadProjectAssemblies ()
_reset = ImageMenuItem (GettextCatalog.GetString ("Reset Shell"))
_reset.Image = Gtk.Image (Stock.Clear, Gtk.IconSize.Menu)
_reset.Activated += def():
if Model.Reset():
resetGui()
_assembliesLoaded = false
if _scriptLines.Length <= 0:
_copyScriptInput.Sensitive = false
_saveScriptToFile.Sensitive = false
_reset.Sensitive = false
if (_assembliesLoaded == false) and (len (getProjectAssemblies ()) > 0):
_loadAssemblies.Sensitive = true
else:
_loadAssemblies.Sensitive = false
_sep = Gtk.SeparatorMenuItem()
menu.Prepend(_sep)
menu.Prepend(_copyScriptInput)
menu.Prepend(_saveScriptToFile)
menu.Prepend(_loadAssemblies)
menu.Prepend(_reset)
_sep.Show()
_copyScriptInput.Show()
_saveScriptToFile.Show()
_loadAssemblies.Show()
_reset.Show()
override def OnKeyPressEvent (ev as Gdk.EventKey):
ka as KeyActions
processkeyresult as bool
// TODO: cast ((char), ev.Key) (seems not to work)
processkeyresult = CompletionWindowManager.PreProcessKeyEvent (ev.Key, char('\0'), ev.State, ka)
CompletionWindowManager.PostProcessKeyEvent (ka)
if processkeyresult:
return true
// Short circuit to avoid getting moved back to the input line
// when paging up and down in the shell output
if ev.Key in (Gdk.Key.Page_Up, Gdk.Key.Page_Down):
return super (ev)
// Needed so people can copy and paste, but always end up
// typing in the prompt.
if Cursor.Compare (InputLineBegin) < 0:
Buffer.MoveMark (Buffer.SelectionBound, InputLineEnd)
Buffer.MoveMark (Buffer.InsertMark, InputLineEnd)
if (ev.State == Gdk.ModifierType.ControlMask) and ev.Key == Gdk.Key.space:
TriggerCodeCompletion ()
if ev.Key == Gdk.Key.Return:
if _inBlock:
if InputLine == "":
processInput (_blockText)
_blockText = ""
_inBlock = false
else:
_blockText += "\n${InputLine}"
if _auto_indent:
_whiteSpace = /^(\s+).*/.Replace(InputLine, "$1")
if InputLine.Trim()[-1:] == ":":
_whiteSpace += "\t"
prompt (true, true)
if _auto_indent:
InputLine += "${_whiteSpace}"
else:
// Special case for start of new code block
if InputLine.Trim()[-1:] == ":":
_inBlock = true
_blockText = InputLine
prompt (true, true)
if _auto_indent:
InputLine += "\t"
return true
// Bookkeeping
if InputLine != "":
// Everything but the last item (which was input),
//in the future stack needs to get put back into the
// past stack
while _commandHistoryFuture.Count > 1:
_commandHistoryPast.Push(cast(string,_commandHistoryFuture.Pop()))
// Clear the pesky junk input line
_commandHistoryFuture.Clear()
// Record our input line
_commandHistoryPast.Push(InputLine)
if _scriptLines == "":
_scriptLines += "${InputLine}"
else:
_scriptLines += "\n${InputLine}"
processInput (InputLine)
return true
// The next two cases handle command history
elif ev.Key == Gdk.Key.Up:
if (not _inBlock) and _commandHistoryPast.Count > 0:
if _commandHistoryFuture.Count == 0:
_commandHistoryFuture.Push(InputLine)
else:
if _commandHistoryPast.Count == 1:
return true
_commandHistoryFuture.Push(cast(string,_commandHistoryPast.Pop()))
InputLine = cast (string, _commandHistoryPast.Peek())
return true
elif ev.Key == Gdk.Key.Down:
if (not _inBlock) and _commandHistoryFuture.Count > 0:
if _commandHistoryFuture.Count == 1:
InputLine = cast(string, _commandHistoryFuture.Pop())
else:
_commandHistoryPast.Push (cast(string,_commandHistoryFuture.Pop()))
InputLine = cast (string, _commandHistoryPast.Peek())
return true
elif ev.Key == Gdk.Key.Left:
// Keep our cursor inside the prompt area
if Cursor.Compare (InputLineBegin) <= 0:
return true
elif ev.Key == Gdk.Key.Home:
Buffer.MoveMark (Buffer.InsertMark, InputLineBegin)
// Move the selection mark too, if shift isn't held
if (ev.State & Gdk.ModifierType.ShiftMask) == ev.State:
Buffer.MoveMark (Buffer.SelectionBound, InputLineBegin)
return true
elif ev.Key == Gdk.Key.period:
ret = super.OnKeyPressEvent(ev)
// prepareCompletionDetails (Buffer.GetIterAtMark (Buffer.InsertMark))
// CompletionListWindow.ShowWindow(char('.'), CodeCompletionDataProvider (_parserContext, _ambience, _fakeFileName, true), self)
return ret
// Short circuit to avoid getting moved back to the input line
// when paging up and down in the shell output
elif ev.Key in (Gdk.Key.Page_Up, Gdk.Key.Page_Down):
return super (ev)
return super (ev)
protected override def OnFocusOutEvent (e as EventFocus):
CompletionWindowManager.HideWindow ()
return super.OnFocusOutEvent(e)
#endregion
private def TriggerCodeCompletion():
iter = Cursor
triggerChar = char('\0')
triggerIter = TextIter.Zero
if (iter.Char != null and iter.Char.Length > 0):
if iter.Char[0] in (char(' '), char('\t'), char('.'), char('('), char('[')):
triggerIter = iter
triggerChar = iter.Char[0]
while iter.LineOffset > 0 and triggerIter.Equals (TextIter.Zero):
if (iter.Char == null or iter.Char.Length == 0):
iter.BackwardChar ()
continue
if iter.Char[0] in (char(' '), char('\t'), char('.'), char('('), char('[')):
triggerIter = iter
triggerChar = iter.Char[0]
break
iter.BackwardChar ()
if (triggerIter.Equals (TextIter.Zero)):
return
triggerIter.ForwardChar ()
// prepareCompletionDetails (triggerIter)
// CompletionListWindow.ShowWindow (triggerChar, CodeCompletionDataProvider (_parserContext, _ambience, _fakeFileName, true), self)
// Mark to find the beginning of our next input line
private _endOfLastProcessing as TextMark
#region Public getters for useful values
public InputLineBegin as TextIter:
get:
endIter = Buffer.GetIterAtMark (_endOfLastProcessing)
return endIter
public InputLineEnd as TextIter:
get:
return Buffer.EndIter
private Cursor as TextIter:
get:
return Buffer.GetIterAtMark (Buffer.InsertMark)
#endregion
// The current input line
public InputLine as string:
get:
return Buffer.GetText (InputLineBegin, InputLineEnd, false)
set:
start = InputLineBegin
end = InputLineEnd
Buffer.Delete (start, end)
start = InputLineBegin
Buffer.Insert (start, value)
#region local private methods
private def processInput (line as string):
Model.QueueInput (line)
private def processOutput (line as string):
end = Buffer.EndIter
Buffer.Insert (end , "\n${line}")
private def prompt (newLine as bool):
prompt (newLine, false)
private def prompt (newLine as bool, multiline as bool):
if newLine:
Buffer.Insert (Buffer.EndIter , "\n")
if multiline:
Buffer.Insert (Buffer.EndIter , "${_promptMultiline}")
else:
Buffer.Insert (Buffer.EndIter , "${_promptRegular}")
Buffer.PlaceCursor (Buffer.EndIter)
ScrollMarkOnscreen(Buffer.InsertMark)
// Record the end of where we processed, used to calculate start
// of next input line
_endOfLastProcessing = Buffer.CreateMark (null, Buffer.EndIter, true)
// Freeze all the text except our input line
Buffer.ApplyTag(Buffer.TagTable.Lookup("Freezer"), Buffer.StartIter, InputLineBegin)
private def resetGui():
if _reset_clears_scrollback:
Buffer.Text = ""
if _reset_clears_history:
_commandHistoryFuture.Clear()
_commandHistoryPast.Clear()
_scriptLines = ""
prompt(not _reset_clears_scrollback)
// FIXME: Make my FileChooser use suck less
private def OnSaveScript():
_sel = SelectFileDialog("Save Script ...", FileChooserAction.Save)
if _sel.Run():
_path = _sel.SelectedFile
using writer = StreamWriter (_path):
writer.Write (_scriptLines)
def OnPropertyChanged (obj as object, e as PropertyChangedEventArgs):
if e.Key == "Font":
self.ModifyFont(Model.Properties.Font)
elif e.Key == "AutoIndentBlocks":
_auto_indent = Model.Properties.AutoIndentBlocks
elif e.Key == "ResetClearsScrollback":
_reset_clears_scrollback = Model.Properties.ResetClearsScrollback
elif e.Key == "ResetClearsHistory":
_reset_clears_history = Model.Properties.ResetClearsHistory
elif e.Key == "LoadAssemblyAfterBuild":
_load_assembly_after_build = Model.Properties.LoadAssemblyAfterBuild
return
#endregion
#region ICompletionWidget
public event CompletionContextChanged as EventHandler;
def ICompletionWidget.CreateCodeCompletionContext (triggerOffset as int) as CodeCompletionContext:
triggerIter = Buffer.GetIterAtOffset (triggerOffset);
rect = GetIterLocation (Buffer.GetIterAtMark (Buffer.InsertMark))
wx as int
wy as int
BufferToWindowCoords (Gtk.TextWindowType.Widget, rect.X, rect.Y + rect.Height, wx, wy)
tx as int
ty as int
GdkWindow.GetOrigin (tx, ty)
ctx = CodeCompletionContext ();
ctx.TriggerOffset = triggerIter.Offset;
ctx.TriggerLine = triggerIter.Line;
ctx.TriggerLineOffset = triggerIter.LineOffset;
ctx.TriggerXCoord = tx + wx;
ctx.TriggerYCoord = ty + wy;
ctx.TriggerTextHeight = rect.Height;
return ctx;
ICompletionWidget.SelectedLength:
get:
select1 as TextIter
select2 as TextIter
if Buffer.GetSelectionBounds (select1, select2):
return Buffer.GetText (select1, select2, true).Length
else:
return 0
ICompletionWidget.TextLength:
get:
return Buffer.EndIter.Offset
def ICompletionWidget.GetChar (offset as int) as System.Char:
return Buffer.GetIterAtLine (offset).Char[0]
def ICompletionWidget.GetText (startOffset as int, endOffset as int) as string:
return Buffer.GetText(Buffer.GetIterAtOffset (startOffset), Buffer.GetIterAtOffset(endOffset), true)
def ICompletionWidget.GetCompletionText (ctx as CodeCompletionContext) as string:
return Buffer.GetText (Buffer.GetIterAtOffset (ctx.TriggerOffset), Buffer.GetIterAtMark (Buffer.InsertMark), false);
def ICompletionWidget.SetCompletionText (ctx as CodeCompletionContext, partial_word as string, complete_word as string):
offsetIter = Buffer.GetIterAtOffset(ctx.TriggerOffset)
endIter = Buffer.GetIterAtOffset (offsetIter.Offset + partial_word.Length)
Buffer.MoveMark (Buffer.InsertMark, offsetIter)
Buffer.Delete (offsetIter, endIter)
Buffer.InsertAtCursor (complete_word)
def ICompletionWidget.Replace (offset as int, count as int, text as string):
offsetIter = Buffer.GetIterAtOffset(offset)
endIter = Buffer.GetIterAtOffset (offsetIter.Offset + count)
Buffer.MoveMark (Buffer.InsertMark, offsetIter)
Buffer.Delete (offsetIter, endIter)
Buffer.InsertAtCursor (text)
ICompletionWidget.GtkStyle:
get:
return self.Style.Copy();
#endregion
|