Tips (vim.iter)

突然ですがニュースです。

ニュースとは言うものの最新ではないです❗最先端からは遅れてます😅

news

Notable changes in Nvim 0.10 from 0.9                                    *news*

For changes in Nvim 0.9, see |news-0.9|.

                                       Type |gO| to see the table of contents.

I once had a girl

Or should I say she once had me

僕にはかつて彼女がいた

いや 彼女には僕がいたと言うべきか 1

Added Features

これちょっと面白いやつ。

:h news-added

ADDED FEATURES                                                     *news-added*

The following new APIs or features were added.

以下の新しいAPIや機能が追加されました。

• vim.iter() provides a generic iterator interface for tables and Lua
iterators luaref-in.

テーブルとLuaのための汎用イテレータインターフェースを提供する

She showed me her room

Isn’t it good Norwegian wood2?

僕は彼女の部屋に招かれた

いいじゃない? ノルウェーの木かな

vim.iter()

書いてあることそのままなんですが、Nvim 0.10からはこんな書き方ができるようになります。

Note

次項より使用しているサンプルコードは 15.11.1 Actions 節で作成したものです。

She asked me to stay

And she told me to sit anywhere

彼女は 泊まっていって と言い

どこでも好きなところに座って と付け加えた

So I looked around

And I noticed there wasn't a chair

僕はあたりを見回したが

椅子なんてものは どこにもない

createTreeActions

Nvim 0.9

local function createTreeActions()
  for _, cmd in pairs(command) do
    table.insert(menuCommand, { name = cmd[3], handler = cmd[2] })
  end
end

Nvim 0.10

local function createTreeActions()
  vim.iter(command):map(function(x)
    table.insert(menuCommand, { handler = x[2], name = x[3] })
  end)
end

誰もが「なんやこいつ😮」と思っていた_がいなくなりました。

I sat on the rug

Biding my time, drinking her wine

僕はラグの上に座り

ワインを飲みながら 時間をやり過ごした

We talked until two

And then she said, "It's time for bed"

二人で夜更けまで語り合うと

やがて 彼女は “もう寝る時間よ” と言った

on_attach

Nvim 0.9

function M.on_attach(bufnr)
  local opts = function(desc)
    return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, nowait = true }
  end

  for _, cmd in pairs(command) do
    if (string.len(cmd[1]) > 0) then
      vim.keymap.set('n', cmd[1], cmd[2], opts(cmd[3]))
    end
  end
end

Nvim 0.10

function M.on_attach(bufnr)
  local opts = function(desc)
    return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, nowait = true }
  end

  vim
    .iter(command)
    :filter(function(x)
      return string.len(x[1]) > 0
    end)
    :map(function(x)
      vim.keymap.set('n', x[1], x[2], opts(x[3]))
    end)
end

上の例はStyluaに素直に従っているのですが、 わたしはこの改行位置に、若干のクセを感じざるを得ません❗

まあ、そこはお好みで😉

She told me she worked in the morning

And started to laugh

彼女は 朝から仕事なの と笑いながら言った

I told her I didn't

And crawled off to sleep in the bath

僕は違うよ と返したが

風呂場で這うようにして眠りにつく

Norwegian Wood (This Bird Has Flown)

And when I awoke I was alone

This bird has flown

目が覚めたら 僕は一人だった

小鳥は飛び去っていたんだ

効率面とか速度面で見た場合に優位性があるのかどうかは、 ぶっちゃけよく知らないんですが...。

Assemble

でも、なんかエレガント🩷👗

So I lit a fire

Isn’t it good Norwegian wood?

だから僕は火をつけてやった

いいじゃない? ノルウェーの木だもんな3