Script Editor +

I've been playing around with Wx::Ruby and made a quick little editor. It's really nothing more than a basic Scintilla editor where I added "quick buttons" that allow you to create classes, modules, methods, etc. with formatting really quick like without having to copy and paste headings and such. Yes, scripters are too lazy to copy and paste! (understandable when you program thousands of lines of code like me 😉 )

Anyways, its not very advanced, or really complete, and I don't see myself doing much more work with this. It serves its purpose for me.

Features:
- Load .rb files
- Save .rb files
- Can show/hide classes/modules/methods/blocks with the left margin
- Adds headings and stuff automatically

Anyways, below is the zip of the exe. I guess I could add some screens to this as well:

SEP 1.0

Screenshots


The indention isn't perfect. I just got annoyed and threw together what I could come up with in a few seconds.

Here is the source code for it. I used Wx::Ruby to make this, and OCRA to compile it together.
  1. #==============================================================================
  2. # ** Script Editor Plus
  3. #------------------------------------------------------------------------------
  4. # SephirothSpawn
  5. # Version 1.0
  6. # 2010-03-01
  7. #==============================================================================
  8.  
  9. #------------------------------------------------------------------------------
  10. # * Require Rubygems & WX::Ruby
  11. #------------------------------------------------------------------------------
  12. require 'rubygems'
  13. require 'wx'
  14.  
  15. #==============================================================================
  16. # * Script Editor Plus
  17. #==============================================================================
  18.  
  19. module SEP
  20.   #--------------------------------------------------------------------------
  21.   # * Frame Sizes
  22.   #--------------------------------------------------------------------------
  23.   List_Width   = 180
  24.   Editor_Width = 700
  25.   #--------------------------------------------------------------------------
  26.   # * Constants
  27.   #--------------------------------------------------------------------------
  28.   Menu_New          = 1000
  29.   Menu_Load         = 1002
  30.   Menu_Save         = 1003
  31.   Menu_About        = 1004
  32.   Menu_Exit         = 1005
  33.   Heading_Class     = 2000
  34.   Heading_Module    = 2001
  35.   Heading_Constants = 2002
  36.   Heading_PIM       = 2003
  37.   Heading_OI        = 2004
  38.   Heading_SMeth     = 2005
  39.   Heading_Meth      = 2006
  40.   Attr_Reader       = 3000
  41.   Attr_Writer       = 3001
  42.   Attr_Accessor     = 3002
  43.   Loop_LoopDo       = 4000
  44.   Loop_Begin        = 4001
  45.   Loop_While        = 4002
  46.   Loop_Until        = 4003
  47.   Loop_For          = 4004
  48.   Exp_If            = 5000
  49.   Exp_Unless        = 5001
  50.   Exp_Case          = 5002
  51.   #--------------------------------------------------------------------------
  52.   # * Keywords
  53.   #--------------------------------------------------------------------------
  54.   Keywords = "begin break elsif module retry unless end case next return until class ensure nil self self. when def false not super while alias defined? for or then yield and do if redo true else in rescue undef"
  55.   #--------------------------------------------------------------------------
  56.   # * Event to Text
  57.   #--------------------------------------------------------------------------
  58.   Text = {}
  59.   Text[Heading_Class] = [
  60.     '#==============================================================================',
  61.     '# ** %s',
  62.     '#==============================================================================',
  63.     '',
  64.     'class %s',
  65.     '',
  66.     'end # End class %s'
  67.   ]
  68.   Text[Heading_Module] = [
  69.     '#==============================================================================',
  70.     '# ** %s',
  71.     '#==============================================================================',
  72.     '',
  73.     'module %s',
  74.     '',
  75.     'end # End module %s'
  76.   ]
  77.   Text[Heading_Constants] = [
  78.     '  #--------------------------------------------------------------------------',
  79.     '  # * Constants',
  80.     '  #--------------------------------------------------------------------------'
  81.   ]
  82.   Text[Heading_PIM] = [
  83.     '  #--------------------------------------------------------------------------',
  84.     '  # * Public Instance Methods',
  85.     '  #--------------------------------------------------------------------------'
  86.   ]
  87.   Text[Heading_OI] = [
  88.     '  #--------------------------------------------------------------------------',
  89.     '  # * Object Initialization',
  90.     '  #--------------------------------------------------------------------------',
  91.     '  def initialize',
  92.     '',
  93.     '  end # End Initialization'
  94.   ]
  95.   Text[Heading_SMeth] = [
  96.     '  #--------------------------------------------------------------------------',
  97.     '  # * %s',
  98.     '  #--------------------------------------------------------------------------',
  99.     '  def self.%s',
  100.     '',
  101.     '  end # End %s'
  102.   ]
  103.   Text[Heading_Meth] = [
  104.     '  #--------------------------------------------------------------------------',
  105.     '  # * %s',
  106.     '  #--------------------------------------------------------------------------',
  107.     '  def %s',
  108.     '',
  109.     '  end # End %s'
  110.   ]
  111.   Text[Loop_LoopDo] = [
  112.     'loop do',
  113.     '  ',
  114.     'end'
  115.   ]
  116.   Text[Loop_Begin] = [
  117.     'begin',
  118.     '  ',
  119.     'end'
  120.   ]
  121.   Text[Loop_While] = [
  122.     'while ',
  123.     '  ',
  124.     'end'
  125.   ]
  126.   Text[Loop_Until] = [
  127.     'until  ',
  128.     '  ',
  129.     'end'
  130.   ]
  131.   Text[Loop_For] = [
  132.     'for _ in ',
  133.     '  ',
  134.     'end'
  135.   ]
  136.   Text[Exp_If] = [
  137.     'if ',
  138.     '  ',
  139.     'end'
  140.   ]
  141.   Text[Exp_Unless] = [
  142.     'unless ',
  143.     '  ',
  144.     'end'
  145.   ]
  146.   Text[Exp_Case] = [
  147.     'case ',
  148.     'when ',
  149.     '  ',
  150.     'end'
  151.   ]
  152.   Text[Attr_Reader] = [
  153.     '  attr_reader :',
  154.   ]
  155.   Text[Attr_Writer] = [
  156.     '  attr_writer :',
  157.   ]
  158.   Text[Attr_Accessor] = [
  159.     '  attr_accessor :',
  160.   ]
  161. end
  162.  
  163. #==============================================================================
  164. # * Script Editor Plus::App
  165. #==============================================================================
  166.  
  167. class SEP::App < Wx::App
  168.   #--------------------------------------------------------------------------
  169.   # * On Initialization
  170.   #--------------------------------------------------------------------------
  171.   def on_init
  172.     SEP::Frame.new('Script Editor Plus').show
  173.   end
  174. end
  175.  
  176. #==============================================================================
  177. # * Script Editor Plus::Frame
  178. #==============================================================================
  179.  
  180. class SEP::Frame < Wx::Frame
  181.   include SEP
  182.   #--------------------------------------------------------------------------
  183.   # * Object Initialization
  184.   #--------------------------------------------------------------------------
  185.   def initialize(title)
  186.     super(nil,-1, title)
  187.     # Setup Menu
  188.     menuFile = Wx::Menu.new()
  189.     menuFile.append(Menu_New,   "&New\tAlt-N",   'New Script')
  190.     menuFile.append(Menu_Load,  "&Load\tAlt-L",  'Load Script')
  191.     menuFile.append(Menu_Save,  "&Save\tAlt-S",  'Save Script')
  192.     menuFile.append(Menu_About, "&About\tAlt-A", 'About')
  193.     menuFile.append(Menu_Exit,  "E&xit\tAlt-X",  'Close Program')
  194.     menuBar = Wx::MenuBar.new()
  195.     menuBar.append(menuFile, "&File")
  196.     set_menu_bar(menuBar)
  197.     evt_menu(Menu_New)   {onNew}
  198.     evt_menu(Menu_Load)  {onLoad}
  199.     evt_menu(Menu_Save)  {onSave}
  200.     evt_menu(Menu_About) {onAbout}
  201.     evt_menu(Menu_Exit)  {onExit}
  202.     # Setup Status Bar
  203.     create_status_bar()
  204.     set_status_text("Welcome to Script Editor Plus!")
  205.     # Create Panels & Adjust Size
  206.     @left_panel= Wx::Panel.new(self, :size => [List_Width, 768], :pos => [0, 0])
  207.     @right_panel = Wx::Panel.new(self, :size => [Editor_Width, 768], :pos => [List_Width, 0])
  208.     self.fit
  209.     centre
  210.     # Create Auto-List
  211.     @list = SEP::List.new(@left_panel, self)
  212.     # Create Editor
  213.     @sci = Wx::StyledTextCtrl.new(@right_panel)
  214.     @sci.style_set_font(Wx::STC_STYLE_DEFAULT, Wx::Font.new(10, Wx::TELETYPE, Wx::NORMAL, Wx::NORMAL))
  215.     @sci.set_size([Editor_Width, 768])
  216.     @sci.set_edge_mode(Wx::STC_EDGE_LINE)
  217.     @sci.set_margin_width(0, @sci.text_width(Wx::STC_STYLE_LINENUMBER, " 9999"))
  218.     @sci.set_tab_width(4)
  219.     @sci.set_use_tabs(true)
  220.     @sci.set_tab_indents(true)
  221.     @sci.set_back_space_un_indents(true)
  222.     @sci.set_indent(4)
  223.     @sci.style_set_foreground(Wx::STC_STYLE_DEFAULT, Wx::BLACK)
  224.     @sci.style_set_background(Wx::STC_STYLE_DEFAULT, Wx::WHITE)
  225.     @sci.style_set_foreground(Wx::STC_STYLE_LINENUMBER, Wx::BLACK);
  226.     @sci.style_set_background(Wx::STC_STYLE_LINENUMBER, Wx::WHITE)
  227.     @sci.style_set_foreground(Wx::STC_STYLE_INDENTGUIDE, Wx::LIGHT_GREY)
  228.     @sci.set_lexer(Wx::STC_LEX_RUBY)
  229.     @sci.style_clear_all
  230.     @sci.style_set_foreground(2, Wx::Colour.new(0, 100, 0))
  231.     @sci.style_set_foreground(3, Wx::RED)
  232.     @sci.style_set_foreground(5, Wx::BLUE)
  233.     @sci.style_set_foreground(6, Wx::Colour.new(150, 10, 110))
  234.     @sci.style_set_foreground(7, Wx::Colour.new(150, 10, 110))
  235.     @sci.set_key_words(0, SEP::Keywords)
  236.     @sci.set_property("fold","1")
  237.     @sci.set_property("fold.compact", "0")
  238.     @sci.set_property("fold.comment", "1")
  239.     @sci.set_property("fold.preprocessor", "1")
  240.     @sci.set_margin_width(1, 0)
  241.     @sci.set_margin_type(1, Wx::STC_MARGIN_SYMBOL)
  242.     @sci.set_margin_mask(1, Wx::STC_MASK_FOLDERS)
  243.     @sci.set_margin_width(1, 20)
  244.     @sci.marker_define(Wx::STC_MARKNUM_FOLDER, Wx::STC_MARK_PLUS)
  245.     @sci.marker_define(Wx::STC_MARKNUM_FOLDEROPEN, Wx::STC_MARK_MINUS)
  246.     @sci.marker_define(Wx::STC_MARKNUM_FOLDEREND, Wx::STC_MARK_EMPTY)
  247.     @sci.marker_define(Wx::STC_MARKNUM_FOLDERMIDTAIL, Wx::STC_MARK_EMPTY)
  248.     @sci.marker_define(Wx::STC_MARKNUM_FOLDEROPENMID, Wx::STC_MARK_EMPTY)
  249.     @sci.marker_define(Wx::STC_MARKNUM_FOLDERSUB, Wx::STC_MARK_EMPTY)
  250.     @sci.marker_define(Wx::STC_MARKNUM_FOLDERTAIL, Wx::STC_MARK_EMPTY)
  251.     @sci.set_fold_flags(16)
  252.     @sci.set_margin_sensitive(1,1)
  253.     # Events
  254.     evt_stc_charadded(@sci.get_id) {|evt| onCharadded(evt)}
  255.     evt_stc_marginclick(@sci.get_id) {|evt| onMarginClick(evt)}
  256.   end
  257.   #--------------------------------------------------------------------------
  258.   # * Menu: New
  259.   #--------------------------------------------------------------------------
  260.   def onNew
  261.     @sci.clear_all
  262.   end
  263.   #--------------------------------------------------------------------------
  264.   # * Menu: Load
  265.   #--------------------------------------------------------------------------
  266.   def onLoad
  267.     dlg = Wx::FileDialog.new(self,  "Choose a file",  '.', :wildcard => '*.rb')
  268.     if dlg.show_modal == Wx::ID_OK
  269.       lines = IO.readlines(dlg.get_path)
  270.       for line in lines
  271.         @sci.add_text(line)
  272.       end
  273.     end
  274.   end
  275.   #--------------------------------------------------------------------------
  276.   # * Menu: Save
  277.   #--------------------------------------------------------------------------
  278.   def onSave
  279.     dlg = Wx::FileDialog.new(self, "Save file as...", '.', "", '*.rb', Wx::SAVE)
  280.     if dlg.show_modal() == Wx::ID_OK
  281.       File.open(dlg.get_path().to_s, 'w+') do |file|
  282.         file.write(@sci.get_text)
  283.       end
  284.     end
  285.   end
  286.   #--------------------------------------------------------------------------
  287.   # * Menu: About
  288.   #--------------------------------------------------------------------------
  289.   def onAbout
  290.     Wx::about_box(:name => self.title, :version => "1.0",
  291.  
  292.         :description => "\nCreate your scripts with Format!", :developers => ['SephirothSpawn'])
  293.   end
  294.   #--------------------------------------------------------------------------
  295.   # * Menu: Exit
  296.   #--------------------------------------------------------------------------
  297.   def onExit
  298.     close(true)
  299.   end
  300.   #--------------------------------------------------------------------------
  301.   # * Item Activated
  302.   #--------------------------------------------------------------------------
  303.   def item_activated(id)
  304.     # Get Class, Module or Method Name
  305.     string = ''
  306.     if [Heading_Class, Heading_Module, Heading_SMeth, Heading_Meth].include?(id)
  307.       dlg = Wx::TextEntryDialog.new(self, 'Please Type Class/Module/Method Name', :default_value => '')
  308.       if dlg.show_modal() == Wx::ID_OK
  309.         string = dlg.get_value.to_s
  310.       elsif dlg.show_modal() == Wx::ID_CANCEL
  311.         return
  312.       end
  313.     end
  314.     # Get Current Indent
  315.     line = @sci.get_line(@sci.get_current_line)
  316.     stripped = line.lstrip
  317.     indent = ' ' * (line.length - stripped.length)
  318.     # For Each Text Line
  319.     lines = Text[id]
  320.     for i in 0...(lines.size)
  321.       line = lines[i]
  322.       @sci.add_text(indent) unless i == 0
  323.       if line.include?('%s')
  324.         @sci.add_text(sprintf(line, string))
  325.       else
  326.         @sci.add_text(line)
  327.       end
  328.       @sci.add_text("\n")
  329.     end
  330.   end
  331.   #--------------------------------------------------------------------------
  332.   # * Event: Character Added
  333.   #--------------------------------------------------------------------------
  334.   def onCharadded(evt)
  335.     chr =  evt.get_key
  336.     curr_line = @sci.get_current_line
  337.     if chr == 13
  338.         if curr_line > 0
  339.           line_ind = @sci.get_line_indentation(curr_line - 1)
  340.           if line_ind > 0
  341.             @sci.set_line_indentation(curr_line, line_ind)
  342.             @sci.goto_pos(@sci.position_from_line(curr_line)+line_ind)
  343.           end
  344.         end
  345.     end
  346.   end
  347.   #--------------------------------------------------------------------------
  348.   # * Event: On Margin Click
  349.   #--------------------------------------------------------------------------
  350.   def onMarginClick(evt)
  351.     line_num = @sci.line_from_position(evt.get_position)
  352.     margin = evt.get_margin
  353.     if margin == 1
  354.       @sci.toggle_fold(line_num)
  355.     end
  356.   end
  357. end
  358.  
  359. #==============================================================================
  360. # * Script Editor Plus::List
  361. #==============================================================================
  362.  
  363. class SEP::List < Wx::TreeCtrl
  364.   include SEP
  365.   #--------------------------------------------------------------------------
  366.   # * Object Initialization
  367.   #--------------------------------------------------------------------------
  368.   def initialize(parent, frame)
  369.     super(parent, 9999, :size => [List_Width, 600])
  370.     @frame = frame
  371.     @list = {}
  372.     root = add_root('Auto-Generators')
  373.     headings = append_item(root, 'Headings')
  374.     attrs = append_item(root, 'Attrs')
  375.     loops = append_item(root, 'Loops')
  376.     exp = append_item(root, 'Expressions')
  377.     @list[append_item(headings, 'Class')] = Heading_Class
  378.     @list[append_item(headings, 'Module')] = Heading_Module
  379.     @list[append_item(headings, 'Constants')] = Heading_Constants
  380.     @list[append_item(headings, 'Public Instance Methods')] = Heading_PIM
  381.     @list[append_item(headings, 'Object Initialization')] = Heading_OI
  382.     @list[append_item(headings, 'Method (self.method)')] = Heading_SMeth
  383.     @list[append_item(headings, 'Method')] = Heading_Meth
  384.     @list[append_item(attrs, 'Reader')] = Attr_Reader
  385.     @list[append_item(attrs, 'Writer')] = Attr_Writer
  386.     @list[append_item(attrs, 'Accessor')] = Attr_Accessor
  387.     @list[append_item(loops, 'Loop Do')] = Loop_LoopDo
  388.     @list[append_item(loops, 'Begin')] = Loop_Begin
  389.     @list[append_item(loops, 'While')] = Loop_While
  390.     @list[append_item(loops, 'Until')] = Loop_Until
  391.     @list[append_item(loops, 'For')] = Loop_For
  392.     @list[append_item(exp, 'If')] = Exp_If
  393.     @list[append_item(exp, 'Unless')] = Exp_Unless
  394.     @list[append_item(exp, 'Case')] = Exp_Case
  395.     expand_all
  396.     evt_tree_sel_changed(9999) {|event| on_select(event)}
  397.     evt_tree_item_activated(9999) {|event| on_activate(event)}    
  398.   end
  399.   #--------------------------------------------------------------------------
  400.   # * On Select
  401.   #--------------------------------------------------------------------------
  402.   def on_select(event)
  403.     @item = event.get_item()
  404.   end
  405.   #--------------------------------------------------------------------------
  406.   # * On Activate
  407.   #--------------------------------------------------------------------------
  408.   def on_activate(event)
  409.     @frame.item_activated(@list[@item]) if @list.has_key?(@item)
  410.   end
  411. end
  412.  
  413. # Run App
  414. SEP::App.new.main_loop()
  415. GC.start
Expand to see the code.


I am not very familiar with Wx::Ruby, so I've probably screwed some areas up and coded more than what I really needed. Feel free to leave whatever comments you wish.
Heh, if you want to make an external script editor, there's really one thing you need for it to be worth it: an intellisense equivalent. That and a resizable editor window >.>

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=70146