Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Add indexForBufferPosition method to EditSession #651

Closed
wants to merge 3 commits into from

Conversation

gjtorikian
Copy link
Contributor

I'm working on creating an Emmet plugin for Atom.

Most of what needs to be done is overriding Emmet functionality to work with Atom's API. Most of the interactions with Emmet expect character index positions in a document. Basically, this necessitates translating [row, column] positions into the index position.

I'd like to add the indexForBufferPosition andbufferPositionForIndex` to translate between the two positions. You can see it used here.

Ideally I would prefer this to go right into Point, but the whole point is that it depends on @buffer to know how to calculate previous line lengths.

#
# Returns the index {Number}.
indexForBufferPosition: (bufferPosition, startRow=0) ->
newlineLength = 1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could probably get rid of this. I was expecting a newLineCharacter variable but it doesn't exist.

@gjtorikian gjtorikian mentioned this pull request Jul 21, 2013
@gjtorikian
Copy link
Contributor Author

I added one more function that you may or may not like: Editor.setTextInRange.

It took me a really long time to realize that TextBuffer.change was exactly something that I was looking for. For the sake of naming consistency (there is getTextInRange), I thought it might be useful to create delegates to change. The Emmet plugin is using this, so that's why I'm bringing it up here.

I could cherry-pick this out if that's preferred.

@gjtorikian
Copy link
Contributor Author

Any comments / feedback / disagreements with this?

Of course I could move the work into the plugin but it seems like a common enough translation for other third-party plugins to place it into core.

@probablycorey
Copy link

We implemented character index <-> buffer position translation for the collaboration branch we are working on. Would https://github.com/github/atom/blob/summit/src/app/text-buffer.coffee#L288 meet your needs?

I like the idea of Editor.setTextInRange! But we should probably rename Editor.getTextInRange and Editor.setTextInRange to Editor.getTextInBufferRange and Editor.setTextInBufferRange and delegate them through EditSession as well. Just to keep everything consistent.

@gjtorikian
Copy link
Contributor Author

@probablycorey Nice, that code worked great.

I'll close this out, and make a reference to change the name into the summit branch (currently using that for work on the plugin).

@gjtorikian gjtorikian closed this Jul 24, 2013
@gjtorikian
Copy link
Contributor Author

Got a branch with the rename work: bcda159937c6^...22bc1c4

A method needs to change in vendor/telepath/lib/shared-string still needs to change to support this. Since it looks like this area of the code is shifting I'll leave it alone for now. I'm 💯 on renaming this or at least even renaming @change to setTextInBufferRange.

@kevinsawicki kevinsawicki deleted the add-index-position branch December 7, 2013 19:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants