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

Vim script 実行時にファイル先頭からの行番号を取得したい #1178

Closed
ichizok opened this issue Jul 18, 2018 · 15 comments
Closed

Comments

@ichizok
Copy link
Member

ichizok commented Jul 18, 2018

関連: #937 #1174

Problem

関数内で現在行のファイル先頭からの行番号を取る簡便な手段がない。
Cレベルでも、実行時の行番号は :source 中しか取れない (はず) 。

Solution proposal

  • ufunc_T 構造体に、定義位置の行番号を記録するフィールドを追加。
  • current_SID と同じように、現在の行番号を記録する変数を追加する (current_SLN) 。

関数定義時に sourcing_lnum を記録し、関数呼び出し時に current_SLNufunc_T から行番号を取得することで、current_SLN + sourcing_lnum が現在行の行番号になる。
この値を返すキーを eval_vars() に追加すればよい。

その他の構造体でも同様に行番号フィールドを追加し、定義時に記録することで、mapautocmd 等で定義位置の行番号を保持できる。

vim/vim@master...ichizok:wip/script_lnum

この branch では、以下の構造体を定義して各構造体の scid_Tsctx_T に置き換えている。

typedef struct {
    scid_T	sc_scid;
    linenr_T	sc_lnum;
} sctx_T;
@ichizok
Copy link
Member Author

ichizok commented Jul 19, 2018

expand('<keyname>') で現在行を取れるようにする。
キーの名前は何がいいでしょうか。

@thinca
Copy link
Member

thinca commented Jul 19, 2018

むずいですね。<flnum> とか? (f は file のつもり)

@tyru
Copy link
Member

tyru commented Jul 19, 2018

<flnum> に一票です!

@ichizok
Copy link
Member Author

ichizok commented Jul 19, 2018

vim/vim@master...ichizok:wip/script_lnum
更新。

:verbose + (set|map|autocmd|command|function) で定義位置の行数を表示

@h-east
Copy link
Member

h-east commented Aug 14, 2018

@ichizok
Do you have any remaining work?
Can you try suggesting to vim_dev?

@blueyed
Copy link

blueyed commented Aug 14, 2018

Thank you for working on this!

I've done a more direct route in vim/vim#3329, but thought about the same like you did here.

I would really love to have a "Source:" line in the ":profile" output, like here: https://github.com/vim/vim/pull/3329/files#diff-7837e28891d0e1dc09cd563abf114dc2R2622

@blueyed
Copy link

blueyed commented Aug 14, 2018

This will add the source to the functions in the profiling output:

diff --git i/src/userfunc.c w/src/userfunc.c
index 0831990b3..ee94c4c85 100644
--- i/src/userfunc.c
+++ w/src/userfunc.c
@@ -2609,6 +2609,9 @@ func_dump_profile(FILE *fd)
                    fprintf(fd, "FUNCTION  <SNR>%s()\n", fp->uf_name + 3);
                else
                    fprintf(fd, "FUNCTION  %s()\n", fp->uf_name);
+               fprintf(fd, "    Source: %s:%li\n",
+                       get_scriptname(fp->uf_sctx.sc_scid),
+                       fp->uf_sctx.sc_lnum);
                if (fp->uf_tm_count == 1)
                    fprintf(fd, "Called 1 time\n");
                else

@blueyed
Copy link

blueyed commented Aug 14, 2018

Have you considered the increased memory usage this will cause?
It adds 8 bytes (for long) to each sourced function, and elsewhere the line number gets stored now.

It is not that much in current times, but maybe this should be enabled explicitly, given that it is only used occasionally.

For the function's line numbers I thought about only tracking them when profiling is enabled/used.

(I have ~2k functions defined according to :func)

@blueyed
Copy link

blueyed commented Aug 14, 2018

Related vim/vim issue: vim/vim#3286

blueyed added a commit to blueyed/vim that referenced this issue Aug 14, 2018
@ichizok
Copy link
Member Author

ichizok commented Aug 20, 2018

@h-east @blueyed Sorry for the late reply.
No test and doc yet so I intended to send PR after adding them.
(Or first better to propose the patch and see Bram's reaction?)

Have you considered the increased memory usage this will cause?

Maybe +10kB~1MB at most. I think it is acceptable cost for the better usability:
This patch can provide users the detailed informations (the line number where option was set, map/func/autocmd/command was defined).

@blueyed
Copy link

blueyed commented Aug 20, 2018

Or first better to propose the patch and see Bram's reaction?

I would recommend that.

What do you think about my patch on top?

@ichizok
Copy link
Member Author

ichizok commented Aug 22, 2018

Sent vim/vim#3362.

@blueyed

What do you think about my patch on top?

Since it changes profile-log format, from the point of the compatibility I didn't include it but introduced as application example.

@ichizok
Copy link
Member Author

ichizok commented Aug 29, 2018

todolisted
vim/vim@fc65cab#diff-38d7929bd26d74d92ceddf984bbfc8dbR142

Patch to add script line number to script ID. (ichizok, Ozaki Kiichi, 2018 Aug 24, #3362)

@ichizok
Copy link
Member Author

ichizok commented Sep 10, 2018

8.1.0362
vim/vim@f29c1c6

@h-east
Copy link
Member

h-east commented Sep 10, 2018

Congrats!
NOTE: key name was changed to <sflnum>.

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

No branches or pull requests

5 participants