1919package org .apache .netbeans .modules .python4nb .editor .lsp ;
2020
2121import java .io .IOException ;
22+ import java .io .InputStream ;
23+ import java .io .OutputStream ;
2224import java .util .HashMap ;
2325import java .util .Map ;
2426import java .util .logging .Level ;
2527import java .util .logging .Logger ;
2628import org .netbeans .api .editor .mimelookup .MimeRegistration ;
2729import org .netbeans .api .editor .mimelookup .MimeRegistrations ;
28- //import org.apache.netbeans.api.project.Project;
29- //import org.netbeans.modules.cpplite.editor.Utils;
3030import org .apache .netbeans .modules .python4nb .editor .file .MIMETypes ;
3131import org .apache .netbeans .modules .python4nb .editor .options .PythonOptions ;
32- import static org .apache .netbeans .modules .python4nb .exec .PythonExecutable .getDefault ;
33- import org .apache .netbeans .modules .python4nb .platform .PythonSupport ;
34- import org .apache .netbeans .modules .python4nb .preferences .PythonPreferences ;
32+ import org .netbeans .api .annotations .common .StaticResource ;
3533import org .openide .util .Exceptions ;
3634import org .openide .util .Pair ;
3735import org .netbeans .api .project .Project ;
3836import org .netbeans .modules .lsp .client .spi .LanguageServerProvider ;
3937import org .openide .util .Lookup ;
40- import org .openide .util .NbPreferences ;
4138
4239/**
4340 *
4441 * @author bresie
4542 */
46- @ MimeRegistrations ({
47- @ MimeRegistration (service = LanguageServerProvider .class ,
48- mimeType = MIMETypes .PY ),
43+ @ MimeRegistrations ({
4944 @ MimeRegistration (service = LanguageServerProvider .class ,
50- mimeType = MIMETypes .PYC ),
45+ mimeType = MIMETypes .PY ),
5146 @ MimeRegistration (service = LanguageServerProvider .class ,
52- mimeType = MIMETypes .PYO )
47+ mimeType = MIMETypes .PYC ),
48+ @ MimeRegistration (service = LanguageServerProvider .class ,
49+ mimeType = MIMETypes .PYO )
5350})
5451public class PythonLsp implements LanguageServerProvider {
5552
5653 private static final Logger LOG = Logger .getLogger (PythonLsp .class .getName ());
5754
5855 private static final Map <Project , Pair <Process , LanguageServerDescription >> prj2Server = new HashMap <>();
56+ @ StaticResource
57+ private static final String PYTHON_ICON = "org/apache/netbeans/modules/python4nb/editor/py.png" ; // NOI18N
5958
6059 @ Override
6160 public LanguageServerDescription startServer (Lookup lookup ) {
6261 LOG .log (Level .INFO , "Starting Python LSP Server" );
63- // Based on CCPLite implementations
64- Project prj = lookup .lookup (Project .class );
65- if (prj == null ) {
66- return null ;
67- }
6862
6963 try {
7064 // get python location
7165 PythonOptions options = PythonOptions .getInstance ();
7266 final String python = options .getPython ();
73- // TODO: get python support from project
67+
68+ // TODO: Add handling when python not configured
69+
70+ // TODO: Check if Python LSP available to be run
71+
72+ // TODO: get python support based on project settings
73+
74+ // Project prj = lookup.lookup(Project.class);
75+ // if (prj == null) {
76+ // return null;
77+ // }
7478 // PythonSupport support = PythonSupport.forProject(prj);
7579 // support = PythonSupport.forProject(prj);
7680 // PythonPreferences pyPreferences = support.getPreferences();
77-
7881 LOG .log (Level .INFO , "Starting up Python LSP Server using {0}" , python );
7982
80- // TODO: Check if Python LSP available to be run
81- Process pythonServer = new ProcessBuilder (python ,
82- "-m" , "pyls" ).start ();
83+ LOG .log (Level .INFO , "Started up Python LSP Server" );
84+ ProcessBuilder pythonServerBuilder = new ProcessBuilder (python , "-m" , "pyls" );
85+ // setup python-language-server with python.exe -m pip install python-language-server
86+ // https://pypi.org/project/python-language-server/
8387
88+ Process pythonServerProcess = pythonServerBuilder .redirectError (ProcessBuilder .Redirect .INHERIT ).start ();
8489 // TODO: If unable to start pyls support need to error out and/or notify user for setup
8590
86- LOG .log (Level .INFO , "Started up Python LSP Server" );
87-
8891 LOG .log (Level .INFO , "Python LSP establish input-output for server." );
89- return LanguageServerDescription .create (pythonServer .getInputStream (),
90- pythonServer .getOutputStream (), pythonServer );
92+ InputStream inputStream = pythonServerProcess .getInputStream ();
93+ OutputStream outputStream = pythonServerProcess .getOutputStream ();
94+ LanguageServerDescription lspDescription
95+ = LanguageServerDescription .create (inputStream , outputStream , pythonServerProcess );
96+ return lspDescription ;
9197 } catch (IOException ex ) {
9298 LOG .log (Level .SEVERE , "Failure on startup of Python LSP server." , ex );
9399 Exceptions .printStackTrace (ex );
94100 return null ;
101+ } catch (Exception ex ) {
102+ LOG .log (Level .SEVERE , "Failure on startup of Python LSP server." , ex );
103+ Exceptions .printStackTrace (ex );
104+ return null ;
95105 }
96106 }
97- }
98- // Based on CCPLite implementations
107+
108+ /* TODO: The below commented code is for reference, once more robust this
109+ code can be removed.
110+
111+ Possible helpful examples
112+ https://github.com/apache/netbeans/blob/master/webcommon/typescript.editor/src/org/netbeans/modules/typescript/editor/TypeScriptLSP.java
113+ Based on CCPLite implementations
114+ */
99115// ServerRestarter restarter = lookup.lookup(ServerRestarter.class);
100116// Utils.settings().addPreferenceChangeListener(new PreferenceChangeListener() {
101117// @Override
@@ -164,7 +180,6 @@ public LanguageServerDescription startServer(Lookup lookup) {
164180// }
165181// return null;
166182// }
167-
168183// public static File getCompileCommandsDir(Project prj) {
169184// return getCompileCommandsDir(getProjectSettings(prj));
170185// }
@@ -247,7 +262,7 @@ public LanguageServerDescription startServer(Lookup lookup) {
247262// return read;
248263// }
249264// }
250- //
265+ //
251266// private static class CopyOutput extends OutputStream {
252267//
253268// private final OutputStream delegate;
@@ -270,6 +285,6 @@ public LanguageServerDescription startServer(Lookup lookup) {
270285// delegate.flush();
271286// log.flush();
272287// }
273- //
288+ //
274289// }
275- // }
290+ }
0 commit comments