Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion java/kotlin.editor/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Manifest-Version: 1.0
AutoUpdate-Show-In-Client: true
OpenIDE-Module: org.netbeans.modules.kotlin.editor
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/kotlin/editor/Bundle.properties
OpenIDE-Module-Specification-Version: 1.28
OpenIDE-Module-Specification-Version: 1.29
OpenIDE-Module-Recommends: org.netbeans.libs.kotlin.lsp
88 changes: 79 additions & 9 deletions java/kotlin.editor/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,77 @@
<specification-version>1.49</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.autoupdate.services</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>1.85</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.editor</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>3</release-version>
<specification-version>1.116</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.editor.lib2</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>2.49</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.editor.mimelookup</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>1.69</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.lsp.client</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>0</release-version>
<specification-version>1.31</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.options.api</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>1.74</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.textmate.lexer</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>0</release-version>
<specification-version>1.2</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.spi.editor.hints</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>0</release-version>
<specification-version>1.71</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.openide.awt</code-name-base>
<build-prerequisite/>
Expand Down Expand Up @@ -74,6 +145,14 @@
<specification-version>7.72</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.openide.modules</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>7.77</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.openide.nodes</code-name-base>
<build-prerequisite/>
Expand Down Expand Up @@ -122,15 +201,6 @@
<specification-version>6.81</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.textmate.lexer</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>0</release-version>
<specification-version>1.2</specification-version>
</run-dependency>
</dependency>
</module-dependencies>
<public-packages/>
</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.kotlin.editor.lsp;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import org.netbeans.api.editor.mimelookup.MimeRegistration;
import org.netbeans.modules.lsp.client.spi.LanguageServerProvider;
import org.netbeans.modules.lsp.client.spi.ServerRestarter;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;

@MimeRegistration(mimeType="text/x-kotlin", service=LanguageServerProvider.class)
public class LanguageServerProviderImpl implements LanguageServerProvider {

@Override
public LanguageServerDescription startServer(Lookup lookup) {
try {
String serverPath = Settings.getKotlinLSPPath();

if (serverPath == null || serverPath.isBlank()) {
return null;
}

File server = new File(serverPath);

if (!server.canExecute()) {
return null;
}

ServerRestarter restarter = lookup.lookup(ServerRestarter.class);
Settings.settings().addPreferenceChangeListener(new PreferenceChangeListener() {
@Override
public void preferenceChange(PreferenceChangeEvent evt) {
restarter.restart();
Settings.settings().removePreferenceChangeListener(this);
}
});

Process p = new ProcessBuilder(server.getAbsolutePath()).redirectError(ProcessBuilder.Redirect.INHERIT).start();
return LanguageServerDescription.create(p.getInputStream(), p.getOutputStream(), p);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.kotlin.editor.lsp;

import java.io.File;
import java.util.prefs.Preferences;
import org.openide.modules.ModuleInfo;
import org.openide.modules.Places;
import org.openide.util.Lookup;
import org.openide.util.NbPreferences;
import org.openide.util.Utilities;

public class Settings {

public static final String KOTLIN_LSP_MODULE_CNB = "org.netbeans.libs.kotlin.lsp";
private static final String KEY_KOTLIN_LSP_PATH = "lsp-path";

public static String getKotlinLSPPath() {
String path = getRawKotlinLSPPath();

if (path.isBlank()) {
return getDefaultPath();
} else {
return path;
}
}

public static String getRawKotlinLSPPath() {
return settings().get(KEY_KOTLIN_LSP_PATH, "");
}

public static void setRawKotlinLSPPath(String path) {
settings().put(KEY_KOTLIN_LSP_PATH, path);
}

public static Preferences settings() {
return NbPreferences.forModule(Settings.class);
}

private static String getDefaultPath() {
String version = null;

for (ModuleInfo mi : Lookup.getDefault().lookupAll(ModuleInfo.class)) {
if (KOTLIN_LSP_MODULE_CNB.equals(mi.getCodeNameBase())) {
version = mi.getSpecificationVersion().toString();
}
}

if (version == null) {
return null;
}

File serverDir = Places.getCacheSubdirectory("kotlin-lsp/" + version);

return new File(new File(serverDir, "bin"), "kotlin-language-server" + (Utilities.isWindows() ? ".bat" : "")).getAbsolutePath();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.kotlin.editor.lsp;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.netbeans.api.autoupdate.PluginInstaller;
import org.netbeans.api.editor.EditorRegistry;
import org.netbeans.modules.editor.NbEditorUtilities;
import org.netbeans.spi.editor.hints.ChangeInfo;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
import org.netbeans.spi.editor.hints.Fix;
import org.netbeans.spi.editor.hints.HintsController;
import org.netbeans.spi.editor.hints.Severity;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.modules.OnStart;
import org.openide.util.Lookup;

@OnStart
public class UnconfiguredHint implements Runnable {

private static final Set<String> KOTLIN_MIME_TYPES = new HashSet<>(Arrays.asList("text/x-kotlin"));

private JTextComponent selectedComponent;

@Override
public void run() {
EditorRegistry.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
updateFocused();
}
});
Settings.settings().addPreferenceChangeListener(new PreferenceChangeListener() {
@Override
public void preferenceChange(PreferenceChangeEvent evt) {
updateFocused();
}
});
updateFocused();
}

private synchronized void updateFocused() {
selectedComponent = EditorRegistry.focusedComponent();
if (selectedComponent == null) {
return ;
}
Document doc = selectedComponent.getDocument();
FileObject file = NbEditorUtilities.getFileObject(doc);
if (file == null || !KOTLIN_MIME_TYPES.contains(FileUtil.getMIMEType(file))) {
return ;
}
List<ErrorDescription> errors = new ArrayList<>();
String lsp = Settings.getKotlinLSPPath();
if (lsp == null || !new File(lsp).canExecute()) {
errors.add(ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "Kotlin LSP server not installed.", Collections.singletonList(new ConfigureLSPServer()), doc, 0));
}
HintsController.setErrors(doc, UnconfiguredHint.class.getName(), errors);
}

private static final class ConfigureLSPServer implements Fix {

@Override
public String getText() {
return "Install Kotlin LSP server";
}

@Override
public ChangeInfo implement() throws Exception {
PluginInstaller.getDefault().install(Settings.KOTLIN_LSP_MODULE_CNB, "Kotlin LSP", Lookup.EMPTY);
return null;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
KotlinOptionsPanel.jLabel1.text=Custom Kotlin LSP Server executable:
KotlinOptionsPanel.browse.text=...
KotlinOptionsPanel.customLSPServerExecutable.text=
Loading
Loading