Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

positionToIndex, indexToPosition methods #1162

Closed
danyaPostfactum opened this issue Dec 18, 2012 · 6 comments
Closed

positionToIndex, indexToPosition methods #1162

danyaPostfactum opened this issue Dec 18, 2012 · 6 comments

Comments

@danyaPostfactum
Copy link
Contributor

I'm looking for the way to convert given indexes (number) to positions ({row:x,column:y} object). It's like selectionStart, selectionEnd of a textarea.
This is my horrible attempt to implement it (outside the ace):

    positionToIndex: function(position) {
      var text = editor.session.getTextRange({start:{column:0,row:0},end: position});
      return text.length;
    },

    indexToPosition: function(index) {
      var lines  = editor.session.getDocument().$lines;
      var newLineChar = editor.session.doc.getNewLineCharacter();
      var currentIndex = 0;
      for (var row = 0; row < lines.length; row ++) {
        var length = editor.session.getLine(row).length;
        if (currentIndex + length >= index) {
          return {
            row: row,
            column: index - currentIndex
          }
        }
        currentIndex += length + newLineChar.length;
      }
    },

Is there a better way to do this? What about the inclusion of these methods into the ACE ?

@faceleg
Copy link

faceleg commented Dec 18, 2012

Out of interest, what are you doing this for? (I had to do a similar thing in ACEView).

On the subject of including such functionality, I suppose they'll suggest you make an extension, as a way to ensure one only includes code used in the given situation. Sorry but I have no idea where to start with that.

@danyaPostfactum
Copy link
Contributor Author

I'm working on intregration emmet into ace. It requres interface without row/column position. I think textarea-like ranges could be useful in some cases.

@nightwing
Copy link
Member

I have started work on emmet some time ago but didn't finish. Here's a cleaned up version e6703dc (works except for proper tabable snippets)

We didn't want to add these functions since they can't be fast on big documents #378 and there are optimizations depending on exact usecase, but they seem to be fast enough and since they are needed frequently, its' better to add them, than have everyone to do the same work

@faceleg
Copy link

faceleg commented Dec 20, 2012

👍 I would love this to be added.

@nightwing
Copy link
Member

opened #1173
@faceleg i will advice against removing aceRange. positionToIndex, indexToPosition methods are slow, they should be used only as a last resort

mostafaeweda pushed a commit that referenced this issue Dec 30, 2012
#1162 positionToIndex, indexToPosition methods
@faceleg
Copy link

faceleg commented Jan 6, 2013

@nightwing thanks for the advice, I will keep the Objective C implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants