MCPcopy
hub / github.com/ternjs/tern_for_vim / tern_rename

Function tern_rename

script/tern.py:349–400  ·  view source on GitHub ↗
(newName)

Source from the content-addressed store, hash-verified

347 return K
348
349def tern_rename(newName):
350 data = tern_runCommand({"type": "rename", "newName": newName}, fragments=False)
351 if data is None: return
352
353 def mycmp(a,b):
354 return (cmp(a["file"], b["file"]) or
355 cmp(a["start"]["line"], b["start"]["line"]) or
356 cmp(a["start"]["ch"], b["start"]["ch"]))
357 data["changes"].sort(key=tern_cmp_to_key(mycmp))
358 changes_byfile = groupby(data["changes"]
359 ,key=lambda c: tern_projectFilePath(c["file"]))
360
361 name = data["name"]
362 changes, external = ([], [])
363 for file, filechanges in changes_byfile:
364
365 buffer = None
366 for buf in vim.buffers:
367 if buf.name == file:
368 buffer = buf
369
370 if buffer is not None:
371 lines = buffer
372 else:
373 with open(file, "r") as f:
374 lines = f.readlines()
375 for linenr, linechanges in groupby(filechanges, key=lambda c: c["start"]["line"]):
376 text = lines[linenr]
377 offset = 0
378 changed = []
379 for change in linechanges:
380 colStart = change["start"]["ch"]
381 colEnd = change["end"]["ch"]
382 text = text[0:colStart + offset] + newName + text[colEnd + offset:]
383 offset += len(newName) - len(name)
384 changed.append({"lnum": linenr + 1,
385 "col": colStart + 1 + offset,
386 "filename": file})
387 for change in changed:
388 if buffer is not None:
389 lines[linenr] = change["text"] = text
390 else:
391 change["text"] = "[not loaded] " + text
392 lines[linenr] = text
393 changes.extend(changed)
394 if buffer is None:
395 with open(file, "w") as f:
396 f.writelines(lines)
397 external.append({"name": file, "text": string.join(lines, ""), "type": "full"})
398 if len(external):
399 tern_sendBuffer(external)
400 vim.command("call setloclist(0," + json.dumps(changes) + ") | lopen")

Callers

nothing calls this directly

Calls 4

tern_runCommandFunction · 0.85
tern_cmp_to_keyFunction · 0.85
tern_projectFilePathFunction · 0.85
tern_sendBufferFunction · 0.85

Tested by

no test coverage detected