Giter Club home page Giter Club logo

Comments (3)

Jugen avatar Jugen commented on June 6, 2024

The following will do it:

// Pattern to see if a line starts with a tab or 1-4 spaces
var tabOrSpacePattern = Pattern.compile( "^(\t| {1,4})" );

codeArea.addEventFilter( KeyEvent.KEY_PRESSED, KE ->
{
    if ( KE.getCode() == KeyCode.TAB )
    {
        var selection = codeArea.getSelection();
        var startParagraph = codeArea.offsetToPosition( selection.getStart(), Bias.Backward ).getMajor();
        var endParagraph = codeArea.offsetToPosition( selection.getEnd(), Bias.Forward ).getMajor();

        if ( startParagraph != endParagraph ) // is it a multi-line selection
        {
            var mc = codeArea.createMultiChange(); // group the changes into one for undo/redo
            IntConsumer tabAction; // takes paragraph number p as a parameter

            if ( ! KE.isShiftDown() ) tabAction = p -> mc.insertText( p, 0, "\t" );
            else tabAction = p -> // remove tabs/spaces if present
            {
                var match = tabOrSpacePattern.matcher( codeArea.getText( p, 0, p, 4 ) );
                if ( match.find() ) mc.deleteText( p, 0, p, match.group().length() );
            };

            for ( var p = startParagraph; p <= endParagraph; p++ )
            {
                if ( codeArea.getParagraph( p ).length() > 0 )
                {
                    tabAction.accept( p );
                }
            }

            mc.commit();

            // Reselect the original selection for further tab actions
            var endLength = codeArea.getParagraph( endParagraph ).length();
            codeArea.selectRange( startParagraph, 0, endParagraph, endLength );

            KE.consume();
        }
    }
} );

from richtextfx.

PavelTurk avatar PavelTurk commented on June 6, 2024

@Jugen Thank you very much. Your solution seems to work without any problems. But why did you add this condition:

if (startParagraph != endParagraph) { // is it a multi-line selection}

This feature must move and only one line, mustn't it?

from richtextfx.

Jugen avatar Jugen commented on June 6, 2024

I was thinking that if the selection is inside a single line then maybe the user wants to replace the selection with a tab and not indent it, so that's why the indenting only activates for multiline selections.

from richtextfx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.