How does it work? It is similar to vim: You simply create a script in the directory $KDEDIR/share/apps/katepart/jscript. An indentation script has to follow several rules:
- it must have a valid script header (the first line must include the string kate-script and indentation scripts must have the type: indentation)
- it must define some variables and functions
- check the indentation script's trigger characters, i.e. whether the script wants to indent code for the typed character
- if yes, call the indentation function
- the return value of the indentation function is an integer value representing the new indentation depth in spaces.
- return value = -1: Kate keeps the indentation, that is it searches for the last non-empty line and uses its indentation for the current line
- return value < -1 tells Kate to do nothing
The name does not really matter, so let's call it foobar.js:
/* kate-scriptMore details on the header:
* name: MyLanguage Indenter
* license: LGPL
* author: Foo Bar
* version: 1
* kate-version: 3.0
* type: indentation
*
*optional bla bla here
*/
// specifies the characters which should trigger indent() beside the default '\n'
triggerCharacters = "{}";
// called for the triggerCharacters {} and
function indent(line, indentWidth, typedChar)
{
// do calculations here
// if typedChar is an empty string, the user hit enter/return
// todo: Implement your indentation algorithms here.return -1; // keep indentation
}
- name [required]: the name will appear in Kate's menus
- license [optional]: not visible in gui, but should be specified in js-files. it is always better to have a defined license
- author [optional]: name
- version [optional]: recommended. an integer
- kate-version [required]: the minimum required kate-version (e.g. for api changes)
- type [required]: must be set to indentation
- kdelibs/kate/jscript/katejscript.cpp search for "@Begin" tags
- document.fileName()
- document.isModified()
- document.charAt(line, column)
- etc...
- view.cursorPosition()
- view.hasSelection()
- view.clearSelection()
- ...
9 comments:
Complex javascript on every character press? Won't that be terribly slow?
Yes and no. As long as we provide the right helper functions in a C++ implementation...
That's the hard part, and that's where help is very much appreciated.
IMHO, using integers as the representation for indentation is fundamentally broken, because it matters whether tabs or spaces (or a mix of both) should be used.
Consider the example:
[TAB]foo(blabla,
Now you want to indent under the 'b' of blabla. The only way to do that which still works when the reader uses a different tab width setting (which is the only reason to use tabs in the first place IMHO!) is to indent the next line with a tab and 4 spaces.
I'm guessing the integer indentation depth represents the depth in `number of indents', so that 2 would mean 4 spaces in a 2-space indentation scheme, or 8 in a 4-space indentation scheme, etc.
@Kevin: You are absolutely right. It is not possible by design. But then, Kate never really supported the mixed mode you mention. And the system as it is right now is really simple.
@Antonio: The returned integer means: Indent X white spaces. Kate Part will transform the white spaces into tabs if it is the user's preference. If you have a tab width of 4 and indent() returns 6, then you will automatically get 1 tab and 2 spaces.
Since the indentation mechanism is going to be redesigned/reimplemented for KDE4, I agree with Kevin that it makes sense to have a scripting engine flexible enough to support the mixed mode he's talking about. In fact, it's one of my main pet peeves with Kate right now.
@Pablo: We are discussing at the kwrite-devel mailing list about it, too.
I'm trying to write a ruby indenter. It actually works pretty well already, but I have run into a problem:
Is there a way to add BACKSPACE to the triggerChars?
I've tried \b \\b \x08 etc, but it doesn't work.
Hello,
and is it possible to use kjs for code completion? If yes, is there a comprehensive guide for that? I gladly appreciate if you drop me a link to qwaser@gmail.com
Post a Comment