Fix disappearing of Python repository from settings after Clion restart (CPP-7743)

Python plugin stored the interpreter setting as a module sdk, which was wiped out with it's order entries by Clion.
The fix is to store the interpreter setting in a facet and reinitialize it after Clion model clearing.
This commit is contained in:
Dmitry Trofimov
2017-02-15 16:03:33 +01:00
parent 298ccc4901
commit 30d86b98da
16 changed files with 466 additions and 95 deletions
@@ -12,12 +12,16 @@
<add-to-group group-id="ToolsMenu" anchor="last"/>
</action>
</actions>
<application-components>
<component>
<interface-class>com.jetbrains.python.console.PythonConsoleRunnerFactory</interface-class>
<implementation-class>com.jetbrains.python.console.PydevConsoleRunnerFactory</implementation-class>
</component>
<component>
<implementation-class>com.jetbrains.python.facet.PythonSdkTableListener</implementation-class>
</component>
</application-components>
</idea-plugin>
@@ -0,0 +1,104 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed 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 com.jetbrains.python.facet;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import static com.jetbrains.python.facet.LibraryContributingFacet.PYTHON_FACET_LIBRARY_NAME_SUFFIX;
/**
* @author traff
*/
public class PythonFacetUtil {
public static String getFacetLibraryName(final String sdkName) {
return sdkName + PYTHON_FACET_LIBRARY_NAME_SUFFIX;
}
public static void updateLibrary(Module module, PythonFacetSettings facetSettings) {
ApplicationManager.getApplication().runWriteAction(() -> {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
final ModifiableRootModel model = rootManager.getModifiableModel();
boolean modelChanged = false;
try {
// Just remove all old facet libraries except one, that is necessary
final Sdk sdk = facetSettings.getSdk();
final String name = (sdk != null) ? getFacetLibraryName(sdk.getName()) : null;
boolean librarySeen = false;
for (OrderEntry entry : model.getOrderEntries()) {
if (entry instanceof LibraryOrderEntry) {
final String libraryName = ((LibraryOrderEntry)entry).getLibraryName();
if (name != null && name.equals(libraryName)) {
librarySeen = true;
continue;
}
if (libraryName != null && libraryName.endsWith(PYTHON_FACET_LIBRARY_NAME_SUFFIX)) {
model.removeOrderEntry(entry);
modelChanged = true;
}
}
}
if (name != null) {
final ModifiableModelsProvider provider = ModifiableModelsProvider.SERVICE.getInstance();
final LibraryTable.ModifiableModel libraryTableModifiableModel = provider.getLibraryTableModifiableModel();
Library library = libraryTableModifiableModel.getLibraryByName(name);
provider.disposeLibraryTableModifiableModel(libraryTableModifiableModel);
if (library == null) {
// we just create new project library
library = PythonSdkTableListener.addLibrary(sdk);
}
if (!librarySeen) {
model.addLibraryEntry(library);
modelChanged = true;
}
}
}
finally {
if (modelChanged){
model.commit();
}
else {
model.dispose();
}
}
});
}
public static void removeLibrary(Module module) {
ApplicationManager.getApplication().runWriteAction(() -> {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
final ModifiableRootModel model = rootManager.getModifiableModel();
// Just remove all old facet libraries
for (OrderEntry entry : model.getOrderEntries()) {
if (entry instanceof LibraryOrderEntry) {
final Library library = ((LibraryOrderEntry)entry).getLibrary();
if (library != null) {
final String libraryName = library.getName();
if (libraryName!=null && libraryName.endsWith(PYTHON_FACET_LIBRARY_NAME_SUFFIX)) {
model.removeOrderEntry(entry);
//PyBuiltinCache.clearInstanceCache();
}
}
}
}
model.commit();
});
}
}
@@ -0,0 +1,104 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed 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 com.jetbrains.python.facet;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModifiableModelsProvider;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.util.messages.MessageBus;
import com.jetbrains.python.sdk.PythonSdkType;
/**
* @author yole
*/
public class PythonSdkTableListener implements Disposable {
public PythonSdkTableListener(MessageBus messageBus) {
ProjectJdkTable.Listener jdkTableListener = new ProjectJdkTable.Listener() {
@Override
public void jdkAdded(final Sdk sdk) {
if (sdk.getSdkType() instanceof PythonSdkType) {
ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runWriteAction(() -> {
addLibrary(sdk);
}));
}
}
@Override
public void jdkRemoved(final Sdk sdk) {
if (sdk.getSdkType() instanceof PythonSdkType) {
removeLibrary(sdk);
}
}
@Override
public void jdkNameChanged(final Sdk sdk, final String previousName) {
if (sdk.getSdkType() instanceof PythonSdkType) {
renameLibrary(sdk, previousName);
}
}
};
messageBus.connect(this).subscribe(ProjectJdkTable.JDK_TABLE_TOPIC, jdkTableListener);
}
static Library addLibrary(Sdk sdk) {
final LibraryTable.ModifiableModel libraryTableModel = ModifiableModelsProvider.SERVICE.getInstance().getLibraryTableModifiableModel();
final Library library = libraryTableModel.createLibrary(PythonFacetUtil.getFacetLibraryName(sdk.getName()));
final Library.ModifiableModel model = library.getModifiableModel();
for (String url : sdk.getRootProvider().getUrls(OrderRootType.CLASSES)) {
model.addRoot(url, OrderRootType.CLASSES);
model.addRoot(url, OrderRootType.SOURCES);
}
model.commit();
libraryTableModel.commit();
return library;
}
private static void removeLibrary(final Sdk sdk) {
ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runWriteAction(() -> {
final LibraryTable.ModifiableModel libraryTableModel =
ModifiableModelsProvider.SERVICE.getInstance().getLibraryTableModifiableModel();
final Library library = libraryTableModel.getLibraryByName(PythonFacetUtil.getFacetLibraryName(sdk.getName()));
if (library != null) {
libraryTableModel.removeLibrary(library);
}
libraryTableModel.commit();
}), ModalityState.NON_MODAL);
}
private static void renameLibrary(final Sdk sdk, final String previousName) {
ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runWriteAction(() -> {
final LibraryTable.ModifiableModel libraryTableModel =
ModifiableModelsProvider.SERVICE.getInstance().getLibraryTableModifiableModel();
final Library library = libraryTableModel.getLibraryByName(PythonFacetUtil.getFacetLibraryName(previousName));
if (library != null) {
final Library.ModifiableModel model = library.getModifiableModel();
model.setName(PythonFacetUtil.getFacetLibraryName(sdk.getName()));
model.commit();
}
libraryTableModel.commit();
}), ModalityState.NON_MODAL);
}
@Override
public void dispose() {
}
}