rsaccon / Bespin playground (http://rsaccon.com/)

No description has been added.

commit 936: 05cd558177a9
parent 870: ae93e926e392
branch: default
tags: tip
added lexical auto indentation and codemirror bugfixes
rsaccon
10 months ago

Changed (Δ3.6 KB):

raw changeset »

frontend/js/bespin/editor/actions.js (58 lines added, 0 lines removed)

frontend/js/bespin/editor/editor.js (1 lines added, 0 lines removed)

frontend/js/bespin/syntax/codemirror/_base.js (27 lines added, 1 lines removed)

Up to file-list frontend/js/bespin/editor/actions.js:

@@ -347,6 +347,64 @@ dojo.declare("bespin.editor.Actions", nu
347
347
        var undoOperation = undoArgs;
348
348
        this.editor.undoManager.addUndoOperation(new bespin.editor.UndoItem(undoOperation, redoOperation));
349
349
    },
350
    
351
    lexicalIndent: function(args) {
352
        if (this.editor.ui.syntaxModel.indent == undefined) {
353
            return;
354
        }
355
            
356
        var historyIndent = args.historyIndent || false;    
357
        var useHistoryIndent = !!historyIndent;
358
        if (!historyIndent) historyIndent = new Array();
359
        var settings = bespin.get('settings');
360
        var selection = args.selection || this.editor.getSelection();
361
        var fakeSelection = args.fakeSelection || false;
362
        var startRow = selection.startPos.row;
363
        var endRow = selection.endPos.row;
364
        var endRowLength = this.cursorManager.getStringLength(this.model.getRowArray(endRow).join(""));
365
        var cursorRowLength = this.cursorManager.getStringLength(this.model.getRowArray(args.pos.row).join(""));
366
        var indents = this.editor.ui.syntaxModel.indent(startRow, endRow, this.editor.language);    
367
        
368
        for (var y = startRow; y <= endRow; y++) {
369
            //console.log(y, " ", indents[y] );
370
            if (indents[y] > 0) {
371
                var charsToInsert;
372
                var tab = ''; 
373
                if (useHistoryIndent) {
374
                    charsToInsert = historyIndent[y - startRow];
375
                } else {
376
                    var tabsize = indents[y];
377
                    while (tabsize-- > 0) {
378
                        tab += " ";
379
                    }
380
                    charsToInsert = tab;
381
                }
382
                this.model.insertCharacters(this.cursorManager.getModelPosition({ row: y, col: 0 }), charsToInsert);
383
                historyIndent[y - startRow] = charsToInsert;                
384
            } else if (indents[y] < 0) {
385
                var charsToDelete;
386
                if (useHistoryIndent) {
387
                     charsToDelete = historyIndent[y - startRow].length;
388
                     charsWidth = (historyIndent[y - startRow] == '\t' ? this.editor.getTabSize() : historyIndent[y - startRow].length);
389
                } else { 
390
                    charsToDelete = -indents[y];
391
                }
392
                if (charsToDelete) {
393
                    this.model.deleteCharacters(this.cursorManager.getModelPosition({ row: y, col: 0 }), charsToDelete);
394
                }
395
            }   
396
        }       
397
        
398
        this.cursorManager.moveCursor({ col: args.pos.col });
399
400
        if (!fakeSelection) {
401
            this.editor.setSelection(selection);
402
        }
403
        historyIndent = historyIndent ? historyIndent : newHistoryIndent;
404
        this.repaint();
405
        
406
        // TODO: undo
407
    }, 
350
408
351
409
    // NOTE: Actually, clipboard.js is taking care of this unless EditorOnly mode is set
352
410
    cutSelection: function(args) {

Up to file-list frontend/js/bespin/editor/editor.js:

@@ -724,6 +724,7 @@ dojo.declare("bespin.editor.UI", null, {
724
724
        listener.bindKeyString("", Key.ENTER, this.actions.newline);
725
725
        listener.bindKeyString("", Key.TAB, this.actions.insertTab);
726
726
        listener.bindKeyString("SHIFT", Key.TAB, this.actions.unindent);
727
        listener.bindKeyString("CTRL", Key.TAB, this.actions.lexicalIndent);
727
728
728
729
        listener.bindKeyString("CMD", Key.A, this.actions.selectAll);
729
730

Up to file-list frontend/js/bespin/syntax/codemirror/_base.js:

@@ -100,6 +100,19 @@ dojo.declare("bespin.syntax.codemirror.M
100
100
        }
101
101
102
102
        return syntaxResults;
103
    },
104
105
    indent: function(startLine, endLine, language) {
106
        var indentations = {}; 
107
   
108
        if (this.language != language) {
109
            this.engine = this.resolver.resolve(language);
110
            this.language = language;
111
        }
112
        for (var currentLine = startLine; currentLine <= endLine; currentLine++) {
113
            indentations[currentLine] = this.engine.indentations[currentLine];
114
        }
115
        return indentations;
103
116
    }
104
117
});
105
118
@@ -164,8 +177,11 @@ dojo.declare("bespin.syntax.codemirror.B
164
177
165
178
    indentUnit: 4,
166
179
167
    // the parser and its frozen state on a per line basis
180
    // Each line caches the parser state
168
181
    parsers: [],
182
                                        
183
    // Each line caches the lexical correct indentation
184
    indentations: [],
169
185
170
186
    // the textlines (minus "\n" at line-end)
171
187
    rows: [],
@@ -507,6 +523,8 @@ dojo.declare("bespin.syntax.codemirror.B
507
523
        var self = this;
508
524
        var lineInfos = {};
509
525
        var from = firstLineToRender;
526
        var firstTokenContent = null; 
527
        var firstTokenStart;
510
528
        var parser;
511
529
512
530
        for (var currentLine = firstLineToRender; currentLine <= lastLineToRender; currentLine++) {
@@ -529,8 +547,16 @@ dojo.declare("bespin.syntax.codemirror.B
529
547
        var parsed = (parser) ? parser(stream) : this.makeParser(stream);
530
548
531
549
        this.forEach.call(this, parsed, function(token){
550
            if ((firstTokenContent === null) && (token.type != "whitespace")) {
551
                 firstTokenContent = token.content;
552
                 firstTokenStart = token.start;  
553
                 console.log(firstTokenContent, " ", firstTokenStart); 
554
            } 
555
            
532
556
            if (token.value == "\n") {
533
557
                self.parsers[token.lineNumber] = parsed.copy();
558
                self.indentations[token.lineNumber] = token.indentation(firstTokenContent) - firstTokenStart;
559
                firstTokenContent = null;
534
560
            }
535
561
536
562
            if (token.lineNumber > lastLineToRender) {