Forum Archive

Questions about editor

Obelisk
  1. I wanna try editor.replace_text, it works well in example, but if i add editor.make_new_file, it wont work after editor.make_new_file, is there any way to make it?
  2. Is there any way to make other language syntax highlight in editor and no need to run?
JonB

in the beta, some other languages highlight in editor.

what is the error you get when using replace_text? can you post your code of what you tried?

Obelisk

In the latest beta,other languages highlight in editor?

there's an example in editor module tourial,it works well,but if i add make_new_file before replace_text it wont work. I will try other functions.

#Comment/Uncomment selected lines

import editor

text = editor.get_text()
selection = editor.get_line_selection()
selected_text = text[selection[0]:selection[1]]
is_comment = selected_text.strip().startswith('#')
replacement = ''
for line in selected_text.splitlines():
    if is_comment:
        if line.strip().startswith('#'):
            replacement += line[line.find('#') + 1:] + '\n'
        else:
            replacement += line + '\n'
    else:
        replacement += '#' + line + '\n'

editor.replace_text(selection[0], selection[1], replacement)
editor.set_selection(selection[0], selection[0] + len(replacement) - 1)

JonB

your best hope of getting help on these forums is to be specific:

-post the code you think should work, not the code you started from with a vague deacription of what you then changed

-tell what you expected to happen, and what atually happened when you ran the code (did you get a crash, an exception, or just unexpect d behavior)

in the latest beta, calling editor.make_new_file with no arguments causes a crash. calling it with a filename creates a file as expected. doing that before calling replace_text makes no sense, because the new file will have no text to replace!

Obelisk

I mean add

editor.make_new_file(tmp, 'test')

editor.set_selection(0, 5)

editor.replace_text(0, 5, 'good')

before replace_text, then all replace_text dont work, how to make it?

ccc

editor.set_selection(0, 4) # the word 'test' contains 4 characters

Obelisk

Thanks, i thought wrong, but as if replace_text dont work yet.

ccc

I am lost...

import editor
editor.make_new_file('tmp', 'test')  # quotes around tmp
editor.set_selection(0, 4)           # 4 instead of 5
editor.replace_text(0, 4, 'good')    # 4 instead of 5

...works as expected in the current v1.6 beta.

JonB

why set selection before replace? im pretty sure replace resets the selection?

ccc

Try taking it out and see... I was surprised myself.

Obelisk

Sorry,i dont use 1.6 beta,please ignore below:

#Comment/Uncomment selected lines

import editor

text = editor.get_text()
selection = editor.get_line_selection()
selected_text = text[selection[0]:selection[1]]
is_comment = selected_text.strip().startswith('#')
replacement = ''
for line in selected_text.splitlines():
    if is_comment:
        if line.strip().startswith('#'):
            replacement += line[line.find('#') + 1:] + '\n'
        else:
            replacement += line + '\n'
    else:
        replacement += '#' + line + '\n'

#import editor
editor.make_new_file('tmp', 'test')  # quotes around tmp
#editor.set_selection(0, 4)           # 4 instead of 5
editor.replace_text(0, 4, 'good')    # 4 instead of 5

editor.replace_text(selection[0], selection[1], replacement)
editor.set_selection(selection[0], selection[0] + len(replacement) - 1)

JonB

ccc, you just have to give the editor a moment to create and open the file before writing to it. for large files, it was a good idea to call get_path until the proper path was returned, but for this example a small sleep works, or even the set selection, etc.

import editor,time
editor.make_new_file('tmp', 'test')  
time.sleep(0.5)
editor.replace_text(0, 4, 'good')    

obelisk...not sure what you are trying to do! are you still having trouble?

Obelisk

Many thanks. Could u give an example to make other language syntax highlight in editor and no need to run?

ccc

Create a new file called junk.html in the Editor (the .html is important) and paste in this content:

<magenta="red">black</magenta> <style type="text/css">/* grey */</style>

The text magenta, red, black, and grey will be in those colors (depending on which theme you have selected).

JonB

syntax highlighting of html is a feature of the beta,not 1.5

Obelisk

Thanks very much,what to do next?use default theme or blackboard theme?