Skip to content

Add convenience method for getting a character's absolute position; add variant for other related methods #330

@JordanMartinez

Description

@JordanMartinez

I believe we've discussed this before, but rather than typing....

int row = 4;
int col = 20;
int charPos = area.position(row, col).toOffset();

... to get the absolute position (which isn't readily obvious to new users of the code) that is necessary for other actions, for example...

area.moveTo(charPos);
area.selectRange(charPos, charPos + 2);
area.replaceText(charPos, charPos - 10, "some text")

Why not provide a convenience method that gets a character's absolute position? This would be more readably apparent than area.position(int, int).toOffset():

public int getAbsoluteCharPosition(int row, int col) { 
    return position(row, col).toOffset(); 
}

(I'm not sure if the supplied method name should be used. absoluteCharPosition might be better?)

Additionally, the above methods could be expanded to include the new row-column format:

public void moveTo(int row, int col) {
    moveTo(getAbsoluteCharPosition(startRow, startCol));
}

public void selectRange(int startRow, int startCol, int endRow, int endCol) {
    int startPos = getAbsoluteCharPosition(startRow, startCol);
    int endPos = getAbsoluteCharPosition(endRow, endCol);
    selectRange(startPos, endPos);
}


public void replaceText(int startRow, int startCol, int endRow, int endCol, String text) {
    int startPos = getAbsoluteCharPosition(startRow, startCol);
    int endPos = getAbsoluteCharPosition(endRow, endCol);
    replaceText(startPos, endPos, text);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions