aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim_lsp/pyls_ms.lua
blob: 3625d80328fe4d36c07c4d9deda171c4ff44391c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
local skeleton = require 'nvim_lsp/skeleton'
local util = require 'nvim_lsp/util'
local lsp = vim.lsp

local name = "pyls_ms"

local function get_python_version()
  local f = io.popen("python --version 2>&1") -- runs command
  local l = f:read("*a") -- read output of command
  f:close()
  return l:match("^Python%s*(...).*%s*$")
end

local function make_installer()
  local P = util.path.join
  local install_dir = P{util.base_install_dir, name}

  local bin = P{install_dir, "Microsoft.Python.LanguageServer.dll"}
  local cmd = {"dotnet", "exec", bin}

  local X = {}
  function X.install()
    local install_info = X.info()
    if install_info.is_installed then
      print(name, "is already installed")
      return
    end
    if not (util.has_bins("curl")) then
      error('Need "curl" to install this.')
      return
    end
    if not (util.has_bins("dotnet")) then
      error('Need ".NET Core" to install this.')
      return
    end

    local system
    if vim.fn.has('mac') == 1 then
      system = 'osx'
    elseif vim.fn.has('unix') == 1 then
      system = 'linux'
    elseif vim.fn.has('win32') == 1 then
      system = 'win'
    else 
      error('Unable to identify host operating system')
    end

    local url = string.format("https://pvsc.azureedge.net/python-language-server-stable/Python-Language-Server-%s-x64.0.4.114.nupkg", string.lower(system))
    download_cmd = string.format('curl -fLo %s --create-dirs %s', install_info.install_dir .. "/pyls.nupkg", url)

    if vim.fn.has('mac') or vim.fn.has('unix') then
      install_cmd = "unzip " .. install_info.install_dir .. "/pyls.nupkg -d " .. install_info.install_dir
    elseif vim.fn.has('win32') then
      install_cmd = "Expand-Archive -Force " .. install_info.install_dir .. "/pyls.nupkg -d " .. install_info.install_dir
    end

    vim.fn.system(download_cmd)
    vim.fn.system(install_cmd)
  end
  function X.info()
    return {
      is_installed = util.path.exists(bin);
      install_dir = install_dir;
      cmd = cmd;
    }
  end
  function X.configure(config)
    local install_info = X.info()
    if install_info.is_installed then
      config.cmd = cmd
    end
  end
  return X
end

local installer = make_installer()

skeleton[name] = {

  default_config = {
    filetypes = {"python"};
    root_dir = function(fname)
      return util.find_git_ancestor(fname) or vim.loop.os_homedir()
    end;
    log_level = lsp.protocol.MessageType.Warning;
    settings = {
      python = {
        analysis = {
          errors = {};
          info = {};
          disabled = {};
        };
      };
    };
    on_new_config = function(config)
      installer.configure(config)
    end;
    init_options = {
      interpreter = 
                {
                    properties= 
                    {
                        InterpreterPath=vim.fn.exepath("python");
                        Version=get_python_version();
                    };
                };
      displayOptions= {};
      analysisUpdates=true;
      asyncStartup=true;
    };
  };
  -- on_new_config = function(new_config) end;
  -- on_attach = function(client, bufnr) end;
  docs = {
    description = [[
https://github.com/Microsoft/python-language-server
`python-language-server`, a language server for Python.
    ]];
    default_config = {
      root_dir = "vim's starting directory";
    };
  };
};

skeleton[name].install = installer.install
skeleton[name].install_info = installer.info