Best way to Update file as user types in

28 views
Skip to first unread message

Stefano Mtangoo

unread,
Jul 19, 2015, 12:00:11 PM7/19/15
to tern...@googlegroups.com
Hi all,

Thank you for your help, my Editor now have a very basic completion but I have a problem on updating file changes as user types in.
Currently all I know is to send whole file.
I want to only send what changed and somehow append to what tern have already.
Does tern provide a way?
If no what is the recommended practice?

Thanks.

Marijn Haverbeke

unread,
Jul 19, 2015, 4:57:40 PM7/19/15
to Stefano Mtangoo, tern-dev
Unless you are dealing with huge files, don't worry about re-sending
the whole file every time. It's fast enough.

For big files (>1000 lines), you can look into sending partial files
(see http://ternjs.net/doc/manual.html#protocol_files). This is
somewhat more involved, but you can look at the other plugins to see
how they do it.
> --
> You received this message because you are subscribed to the Google Groups
> "tern-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to tern-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Stefano Mtangoo

unread,
Jul 19, 2015, 5:08:27 PM7/19/15
to tern...@googlegroups.com, mwinj...@gmail.com


On Sunday, July 19, 2015 at 10:57:40 PM UTC+2, Marijn Haverbeke wrote:
Unless you are dealing with huge files, don't worry about re-sending
the whole file every time. It's fast enough.
So I tried with a very small file. Here is what I do:
1. Send file code (I read text and send it instead of just file)
2. I query the autocomps
It does not work. I followed one of threads here and am sending TEXT something like
{"files":[{"text":"EDITOR CURRENT CONTET HERE","name":"simple.js","type":"full"}]}

I have to wait until server parses everything and issue autocomplete,
File is super small and I believe am doing something wrong.

Is this how I do it?
 

For big files (>1000 lines), you can look into sending partial files
(see http://ternjs.net/doc/manual.html#protocol_files). This is
somewhat more involved, but you can look at the other plugins to see
how they do it.
I will read and poke them to see what I can make out of it
Thanks

Stefano Mtangoo

unread,
Jul 23, 2015, 4:34:46 AM7/23/15
to tern-dev, mwinj...@gmail.com
Hi Marijn,
I have been over this week trying to figure out how to send partial files (using sublime text plugin as reference) but really can't get it to work. Here is my scenario:
1. User opens big js file -- I register with tern [This works fineand I can get Autocomplete]
Current position on Editor is 390.

2. User Types 17 characters and requests autocomplete -- I take Text that was added, that is between position 390 to 407
I send the Text as well as offset which in this case (as I understand it) is position 390

3. I query the autocompletions -- I get error [Response: 400 TernError: Position 407 is outside of file.]

What do I do wrong?

Here is Whole log for this simple file I use. Also you can read it at pastebin

10:14:44: Listening on port 8000
10:14:47: Request: {
10:14:48:   "files": [
10:14:48:     {
10:14:49:       "name": "/home/stefano/me.js",
10:14:49:       "text": "var Person = Class.extend({\n  init: function(isDancing){\n    this.dancing = isDancing;\n  },\n  dance: function(){\n    return this.dancing;\n  }\n});\n \nvar Ninja = Person.extend({\n  init: function(){\n    this._super( false );\n  },\n  dance: function(){\n    // Call the inherited version of dance()\n    return this._super();\n  },\n  swingSword: function(){\n    return true;\n  }\n});\n \nvar pp = new Ninja(); ",
10:14:50:       "type": "full"
10:14:50:     }
10:14:51:   ]
10:14:51: }
10:14:52: Response: {}
10:14:52:
10:14:58: Request: {
10:14:59:   "query": {
10:14:59:     "caseInsensitive": true,
10:15:00:     "end": 390,
10:15:00:     "file": "/home/stefano/me.js",
10:15:01:     "includeKeywords": true,
10:15:01:     "lineCharPositions": true,
10:15:02:     "type": "completions",
10:15:02:     "types": true,
10:15:03:     "urls": true
10:15:03:   },
10:15:04:   "timeout": null
10:15:04: }
10:15:05: Response: {
10:15:05:   "start": {
10:15:06:     "line": 22,
10:15:06:     "ch": 13
10:15:07:   },
10:15:07:   "end": {
10:15:08:     "line": 22,
10:15:08:     "ch": 18
10:15:09:   },
10:15:09:   "isProperty": false,
10:15:10:   "isObjectKey": false,
10:15:10:   "completions": [
10:15:11:     {
10:15:11:       "name": "Ninja",
10:15:12:       "guess": true,
10:15:12:       "type": "fn()"
10:15:13:     }
10:15:13:   ]
10:15:14: }
10:15:14:
10:15:15: Request: {
10:15:15:   "files": [
10:15:16:     {
10:15:16:       "name": "/home/stefano/me.js",
10:15:17:       "offset": 390,
10:15:17:       "text": "Ninja(false);\npp.",
10:15:18:       "type": "part"
10:15:18:     }
10:15:19:   ]
10:15:19: }
10:15:20: Response: {}
10:15:20:
10:15:21: Request: {
10:15:21:   "query": {
10:15:22:     "caseInsensitive": true,
10:15:22:     "end": 407,
10:15:23:     "file": "/home/stefano/me.js",
10:15:23:     "includeKeywords": true,
10:15:24:     "lineCharPositions": true,
10:15:24:     "type": "completions",
10:15:25:     "types": true,
10:15:25:     "urls": true
10:15:26:   },
10:15:26:   "timeout": null
10:15:27: }
10:15:27: Response: 400 TernError: Position 407 is outside of file.




Marijn Haverbeke

unread,
Jul 25, 2015, 4:52:39 PM7/25/15
to Stefano Mtangoo, tern-dev
I wrote about all this stuff in the manual, which was a lot of work.
It'd be great if your questions showed that you had at least read that
manual. To wit, you need to refer to `file: "#0"` in your query to
refer to code in fragments, and include that fragment in the same
request. You also need to include more than just a few characters, so
that the parser can make sense of what you sent. Also, read existing
plugins, so and copy what they do, if you want to get this right.

Stefano D. Mtangoo

unread,
Jul 25, 2015, 5:25:40 PM7/25/15
to Marijn Haverbeke, tern-dev
Hi,

On 25/07/2015 10:52 alasiri, Marijn Haverbeke wrote:
I wrote about all this stuff in the manual, which was a lot of work.
It'd be great if your questions showed that you had at least read that
manual. 
Sorry if my question does not show that. But to be explicit, I have read it more that 10 times. There are some things I cannot understand (or I misunderstand).

To wit, you need to refer to `file: "#0"` in your query to
refer to code in fragments, and include that fragment in the same
request. You also need to include more than just a few characters, so
that the parser can make sense of what you sent. 
Thank you for the tip. I will work on that.

Also, read existing
plugins, so and copy what they do, if you want to get this right.
I tried to read ST plugin as I'm good on Python but I cannot understand the Sublime specific things, so sometimes I miss some things.
I will read it again (with aboved tip) to see where I can go.

Again thank you for your help!
Stefano
--
Stefano D. Mtangoo
Mob: +255 754710410
Twitter: @mtangoo
Web. http://hosannahighertech.co.tz
Linkedin: http://www.linkedin.com/pub/stefano-mtangoo/45/644/281
The purpose of man is to know his Maker Be known by his Maker
And make his Maker known So that others may know his Maker as their Maker(Emeal Zwayne)
Reply all
Reply to author
Forward
0 new messages