Page 1 of 3
Editing RubyEdit class
Posted: Tue Aug 19, 2014 10:53 am
by Exo
I noticed in the Flowstone install folder there is an fsm file called "system" in this schematic is a Ruby component that defines the RubyEdit class. I was thinking could we not alter this class to add line numbers ect?
I have played about with it a bit changing a few things in the syntax colouring ect, but couldn't get any line numbering going.
Can someone else that is fluent in Ruby see if that can be done?
Cheers,
Exo
Re: Editing RubyEdit class
Posted: Tue Aug 19, 2014 4:21 pm
by Nubeat7
yes it should work with codeRay
this shows how to get the line numbers
Code: Select all
CodeRay.scan("5.times do\n puts 'Hello, world!'\nend", :ruby).div(:line_numbers => :table)
but i don't know hoe to put them in?
http://coderay.rubychan.de/
Re: Editing RubyEdit class
Posted: Tue Aug 19, 2014 7:29 pm
by Exo
Thanks.
Seems div is outputing HTML which will be why it doesn't work.
I have tried modifying a few things but no luck....
Code: Select all
require "coderay"
class RubyEdit
Black = invertedColours ? [255,255,255] : [0,0,0]
Brown = [128,64,0]
Red = [163,20,20]
Blue = [0,0,255]
Green = [0,128,0]
Pink = [200,0,200]
Gold = [128,128,0]
Purple = [100,0,200]
Grey50 = [128,128,128]
Silver = [100,100,64]
def parse v,f
sourceLines = Array.new
v.each_line {|s| sourceLines.push s}
startIndex=0
lineStartIndeces=[0]
startIndex=v.index("\n",0)
while startIndex
lineStartIndeces<<(startIndex+1)
startIndex=v.index("\n",startIndex+2)
end
v.force_encoding "utf-8"
tp=CodeRay.scan(v,:ruby).div(:line_numbers => :table) #Doesn't work it outputs HTML
lines=[]
nLines=sourceLines.count
nLines.times {lines<<[]}
ci=0
strings=[]
types=[]
tp.each_with_index do |t,i|
if i.even? then strings<<t end
if i.odd? then types<<t end
# Check for newlines inside the current string and split
# where necessary
if i.odd?
if t != :string && t != :space && strings.last.class == String
subs = strings.last.split("\n")
if subs.size > 0
s = strings.pop
t = types.pop
subs.each do |str|
if str.size > 0
strings<<str
types<<t
end
strings<<"\n"
types<<:space
end
if s.index("\n") != s.length-1
s = strings.pop
t = types.pop
end
end
end
end
end
ln=0
for i in 0..strings.count-1
item=[]
t=types[i]
s=strings[i]
if s.eql? "\n"
ln+=1
elsif t!=:space && s.class==String
i1=v.index(s,ci)
if i1
ci=i1+s.length
item<<i1-lineStartIndeces[ln]<<s.length<<t
if ln<nLines
lines[ln]<<item
end
end
end
end
# Set up colour scheme
m={ :ident=>Black,:delimeter=>Black,:operator=>Black,
:keyword=>Blue,:class=>Pink,:method=>Black,:comment=>Green,
:class_variable=>Gold,:instance_variable=>Gold,:global_variable=>Silver,
:symbol=>Grey50,:constant=>Pink,
:integer=>Red,:float=>Red,:char=>Red,:content=>Red,:string=>Pink,
:regexp=>Black,:shell=>Black,:schematic=>Purple }
# Set up our own commands
schemCmds = ["output","input","watch","releaseMouse","captureMouse",
"redraw","time","parent","caption","alert","showCursor"]
li=0
# Colour the lines
lines.each do |l|
f.newLine
os = 0
if li < sourceLines.size
len = sourceLines[li].chomp.length
end
l.each do |i|
t=i[2]
# Check if we ran onto another line (due to a line continuation '\')
if i[0]-os > len
f.endLine len
os += len+2
f.newLine
li+=1
if li < sourceLines.size
len = sourceLines[li].chomp.length
end
end
if (i[0]+i[1]-os-1) < len
if( t == :ident && schemCmds.include?(sourceLines[li][(i[0]-os)..(i[0]-os+i[1]-1)]) )
t = :schematic
end
f.addSection i[0]-os,i[0]-os+i[1]-1,m[t]
end
end
f.endLine len
li+=1
end
end
end
Re: Editing RubyEdit class
Posted: Tue Aug 19, 2014 8:33 pm
by Nubeat7
yes, it outputs html...
i think the main problem is that codeRay is able to spitout line nrs, but what we need would be a nr table in the editor itself which would need to be an own area beside the editor area itself..
so we cannot add something to any v.source because it would distroy the code or?
i was thinking about to add a nr after each \n which doesn't affect the code behind - like only whats behind that is valid code - it but i`ve no idea if this would be possible?
Re: Editing RubyEdit class
Posted: Tue Aug 19, 2014 9:37 pm
by Exo
Nubeat7 wrote:yes, it outputs html...
i think the main problem is that codeRay is able to spitout line nrs, but what we need would be a nr table in the editor itself which would need to be an own area beside the editor area itself..
so we cannot add something to any v.source because it would distroy the code or?
i was thinking about to add a nr after each \n which doesn't affect the code behind - like only whats behind that is valid code - it but i`ve no idea if this would be possible?
I don't know if it is possible everything I am currently trying breaks the code.
I have found a new class though LineFormat , that is the f variable.
I think it should be doable but currently I am struggling to understand the code and so I cannot say for definite.
Edit... I actually yes I think you are right, I don't think it is possible because adding extra characters would break the code that is passed to the interpreter.
Was a nice idea though I just didn't understand the code until now.
Re: Editing RubyEdit class
Posted: Thu Aug 21, 2014 1:18 pm
by Nubeat7
but would it be possible to show the actual code in an extra window including linenr?
i mean if we send self (from the window where some code is written) into another ruby module which just draws the code as text (including linenr) using coderay?
this would also be interesting for documetation as coderay enables you to filter out comments too, so you could generate a textfile which includes all the comments of your code.
Re: Editing RubyEdit class
Posted: Thu Aug 21, 2014 2:33 pm
by Exo
I think that should be possible.
The coderay lines doesn't seem to work. To work we would need to copy the original code as a new string object and append the number to the beginning of each line manually and then this could be displayed.
Re: Editing RubyEdit class
Posted: Thu Aug 21, 2014 8:53 pm
by Nubeat7
hmm, got some troubles with encoding to utf-8!?
here is a schematic of how i think it could work convert code into txt (if this ascii 8bit to utf-8 encoding would work)
this
also only works if you include the encoders folder form the original coderay
zip file into the flowstone ruby folder:..ruby/libraries/syntax/coderay !! which is not included in FS
.encode does scan input (v) to language(:ruby) and output to given format (:text) all in one, which i think should be the easiest way if i'm right with all that..
Re: Editing RubyEdit class
Posted: Thu Aug 21, 2014 9:11 pm
by trogluddite
Exo wrote:To work we would need to copy the original code as a new string object and append the number to the beginning of each line manually and then this could be displayed.
Yes, and there is a sneaky way to do it.
Systems are down at home at the moment for some home improvements, but I have it working and can post later.
The trick is to hijack a RubyEdit method called "parse" - it gets called every time the code is edited. IIRC, I intercepted the input arguments by doing something like this...
Code: Select all
def parse(*args, &block)
@code_string = args[0] # May not be the right index!
super(*args, &block) # Hand control back to the original method
end
@Exo - take a look in the code tracer prototype I sent you. it is in there somewhere.
Re: Editing RubyEdit class
Posted: Thu Aug 21, 2014 9:27 pm
by Exo
Nubeat7 wrote:hmm, got some troubles with encoding to utf-8!?
here is a schematic of how i think it could work convert code into txt (if this ascii 8bit to utf-8 encoding would work)
this
also only works if you include the encoders folder form the original coderay
zip file into the flowstone ruby folder:..ruby/libraries/syntax/coderay !! which is not included in FS
.encode does scan input (v) to language(:ruby) and output to given format (:text) all in one, which i think should be the easiest way if i'm right with all that..
Nice Idea but the encoding cannot work because the RubyEdit is it's own object it isn't a String object and so cannot be encoded.
That file crashes flowstone here if I double click the file instead of opening it inside FS.