use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.
the class DjangoProjectProperties method createContents.
@Override
protected Control createContents(Composite parent) {
project = getElement().getAdapter(IProject.class);
Composite topComp = new Composite(parent, SWT.NONE);
GridLayout innerLayout = new GridLayout();
innerLayout.numColumns = 2;
innerLayout.marginHeight = 0;
innerLayout.marginWidth = 0;
topComp.setLayout(innerLayout);
GridData gd = new GridData(GridData.FILL_BOTH);
topComp.setLayoutData(gd);
if (project != null) {
try {
IPythonPathNature pathNature = PythonNature.getPythonPathNature(project);
final PythonNature nature = PythonNature.getPythonNature(project);
Map<String, String> variableSubstitution = pathNature.getVariableSubstitution(false);
Label label = new Label(topComp, SWT.None);
label.setText("Django manage.py");
Text text = new Text(topComp, SWT.BORDER);
textDjangoManage = text;
textDjangoManage.setToolTipText("This is the name of the project-relative location of manage.py (i.e.: src/myapp/manage.py)");
label = new Label(topComp, SWT.None);
labelErrorManage = new Label(topComp, SWT.None);
labelErrorManage.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
ModifyListener manageValidator = new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
try {
String path = textDjangoManage.getText().trim();
if (path.trim().length() == 0) {
labelErrorSettings.setText("Please specify the manage.py relative name (i.e.: src/myapp/manage.py)");
return;
}
IFile file = project.getFile(new Path(path));
if (!file.exists()) {
labelErrorManage.setText(StringUtils.format("File: %s could not be found.", path));
} else {
labelErrorManage.setText("");
}
} catch (Exception e1) {
Log.log(e1);
}
}
};
text.addModifyListener(manageValidator);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
String string = variableSubstitution.get(DjangoConstants.DJANGO_MANAGE_VARIABLE);
if (string != null) {
text.setText(string);
} else {
text.setText("");
}
// Settings
label = new Label(topComp, SWT.None);
label.setText("Django settings module");
text = new Text(topComp, SWT.BORDER);
textDjangoSettings = text;
textDjangoSettings.setToolTipText("This is the name of the django settings module (i.e.: myapp.settings)");
label = new Label(topComp, SWT.None);
labelErrorSettings = new Label(topComp, SWT.None);
labelErrorSettings.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
ModifyListener settingsValidator = new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
try {
String moduleName = textDjangoSettings.getText().trim();
if (moduleName.trim().length() == 0) {
labelErrorSettings.setText("Please specify the name of the module (i.e.: myapp.settings)");
return;
}
ICodeCompletionASTManager astManager = nature.getAstManager();
ProjectModulesManager modulesManager = (ProjectModulesManager) astManager.getModulesManager();
IModule moduleInDirectManager = modulesManager.getModuleInDirectManager(moduleName, nature, true, new BaseModuleRequest(false));
if (moduleInDirectManager == null) {
labelErrorSettings.setText(StringUtils.format("Module: %s could not be found.", moduleName));
} else {
labelErrorSettings.setText("");
}
} catch (Exception e1) {
Log.log(e1);
}
}
};
text.addModifyListener(settingsValidator);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
string = variableSubstitution.get(DjangoConstants.DJANGO_SETTINGS_MODULE);
if (string != null) {
text.setText(string);
} else {
text.setText("");
}
} catch (Exception e) {
Log.log(e);
}
} else {
Label label = new Label(topComp, SWT.None);
label.setText("Internal error: project not set!");
}
return topComp;
}
use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.
the class DjangoProjectWizard method createAndConfigProject.
@Override
protected void createAndConfigProject(IProject projectHandle, IProjectDescription description, String projectType, String projectInterpreter, IProgressMonitor monitor, Object... additionalArgsToConfigProject) throws CoreException {
Assert.isTrue(additionalArgsToConfigProject.length == 1);
final DjangoSettings djSettings = (DjangoSettings) additionalArgsToConfigProject[0];
final int sourceFolderConfigurationStyle = projectPage.getSourceFolderConfigurationStyle();
ICallback<List<IContainer>, IProject> getSourceFolderHandlesCallback = super.getSourceFolderHandlesCallback;
ICallback<List<IPath>, IProject> getExistingSourceFolderHandlesCallback = super.getExistingSourceFolderHandlesCallback;
ICallback<Map<String, String>, IProject> getVariableSubstitutionCallback = new ICallback<Map<String, String>, IProject>() {
@Override
public Map<String, String> call(IProject projectHandle) {
Map<String, String> variableSubstitution = new HashMap<String, String>();
String manageLocation;
if (djSettings.djangoVersion.equals(DjangoSettingsPage.DJANGO_14)) {
manageLocation = "manage.py";
} else {
// Before 1.4
manageLocation = projectHandle.getName() + "/manage.py";
}
switch(sourceFolderConfigurationStyle) {
case IWizardNewProjectNameAndLocationPage.PYDEV_NEW_PROJECT_CREATE_PROJECT_AS_SRC_FOLDER:
case IWizardNewProjectNameAndLocationPage.PYDEV_NEW_PROJECT_NO_PYTHONPATH:
break;
default:
manageLocation = "src/" + manageLocation;
}
variableSubstitution.put(DjangoConstants.DJANGO_MANAGE_VARIABLE, manageLocation);
return variableSubstitution;
}
};
PyStructureConfigHelpers.createPydevProject(description, projectHandle, monitor, projectType, projectInterpreter, getSourceFolderHandlesCallback, null, getExistingSourceFolderHandlesCallback, getVariableSubstitutionCallback);
// The django nature is added only so that we can identify whether we should show django actions.
DjangoNature.addNature(projectHandle, null);
try {
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
PythonNature nature = PythonNature.getPythonNature(projectHandle);
Assert.isNotNull(nature);
ICodeCompletionASTManager astManager = nature.getAstManager();
Object sync = new Object();
// Wait up to 10 seconds for it to be restored
for (int i = 0; i < 100 && astManager == null; i++) {
synchronized (sync) {
try {
sync.wait(100);
} catch (InterruptedException e) {
}
}
astManager = nature.getAstManager();
}
if (astManager == null) {
throw new RuntimeException("Error creating Django project. ASTManager not available after 10 seconds.\n" + "Please report this bug at the sourceforge tracker.");
}
AbstractRunner runner = UniversalRunner.getRunner(nature);
IContainer projectContainer;
switch(sourceFolderConfigurationStyle) {
case IWizardNewProjectNameAndLocationPage.PYDEV_NEW_PROJECT_CREATE_PROJECT_AS_SRC_FOLDER:
case IWizardNewProjectNameAndLocationPage.PYDEV_NEW_PROJECT_EXISTING_SOURCES:
case IWizardNewProjectNameAndLocationPage.PYDEV_NEW_PROJECT_NO_PYTHONPATH:
projectContainer = projectHandle;
break;
default:
projectContainer = projectHandle.getFolder("src");
}
String projectName = projectHandle.getName();
Tuple<String, String> output = runner.runCodeAndGetOutput(RUN_DJANGO_ADMIN, new String[] { "startproject", projectName }, projectContainer.getLocation().toFile(), new NullProgressMonitor());
if (output.o2.indexOf("ImportError: no module named django") != -1) {
RunInUiThread.async(new Runnable() {
@Override
public void run() {
MessageDialog.openError(EditorUtils.getShell(), "Unable to create project.", "Unable to create project because the selected interpreter does not have django.");
}
});
projectHandle.delete(true, null);
return;
}
IDocument docFromResource = null;
IFile settingsFile = null;
if (djSettings.djangoVersion.equals(DjangoSettingsPage.DJANGO_14)) {
// Ok, Django 1.4 is now as follows:
// It'll create a structure
// /projectName
// /projectName/manage.py
// /projectName/projectName
// /projectName/projectName/__init__.py
// /projectName/projectName/settings.py
// So, what pydev did before (i.e.: creating the projectName inital folder) is repeated in its process.
// Thus, we have to go on and get rid of it, moving the manage.py and projectName to the projectContainer
// and removing the root projectName altoghether.
File copyTo = projectContainer.getLocation().toFile();
File copyFrom = new File(copyTo, projectName);
File[] files = copyFrom.listFiles();
if (files != null) {
for (File f : files) {
if (f.isFile()) {
try {
FileUtils.copyFile(f, new File(copyTo, f.getName()));
FileUtils.deleteFile(f);
} catch (Exception e) {
Log.log(e);
}
} else {
try {
FileUtils.copyDirectory(f, new File(copyTo, f.getName()), null, null);
FileUtils.deleteDirectoryTree(f);
} catch (Exception e) {
Log.log(e);
}
}
}
}
}
// Before 1.4
settingsFile = projectContainer.getFile(new Path(projectName + "/settings.py"));
settingsFile.refreshLocal(IResource.DEPTH_ZERO, null);
docFromResource = FileUtilsFileBuffer.getDocFromResource(settingsFile);
if (docFromResource == null) {
throw new RuntimeException("Error creating Django project.\n" + "settings.py file not created.\n" + "Stdout: " + output.o1 + "\n" + "Stderr: " + output.o2);
}
String settings = docFromResource.get();
if (djSettings.djangoVersion.equals(DjangoSettingsPage.DJANGO_12_OR_13) || djSettings.djangoVersion.equals(DjangoSettingsPage.DJANGO_14)) {
// 1.2, 1.3 or 1.4
settings = settings.replaceFirst("'ENGINE': 'django.db.backends.'", "'ENGINE': 'django.db.backends." + djSettings.databaseEngine + "'");
settings = settings.replaceFirst("'NAME': ''", "'NAME': '" + djSettings.databaseName + "'");
settings = settings.replaceFirst("'HOST': ''", "'HOST': '" + djSettings.databaseHost + "'");
settings = settings.replaceFirst("'PORT': ''", "'PORT': '" + djSettings.databasePort + "'");
settings = settings.replaceFirst("'USER': ''", "'USER': '" + djSettings.databaseUser + "'");
settings = settings.replaceFirst("'PASSWORD': ''", "'PASSWORD': '" + djSettings.databasePassword + "'");
} else {
// Before 1.2
settings = settings.replaceFirst("DATABASE_ENGINE = ''", "DATABASE_ENGINE = '" + djSettings.databaseEngine + "'");
settings = settings.replaceFirst("DATABASE_NAME = ''", "DATABASE_NAME = '" + djSettings.databaseName + "'");
settings = settings.replaceFirst("DATABASE_HOST = ''", "DATABASE_HOST = '" + djSettings.databaseHost + "'");
settings = settings.replaceFirst("DATABASE_PORT = ''", "DATABASE_PORT = '" + djSettings.databasePort + "'");
settings = settings.replaceFirst("DATABASE_USER = ''", "DATABASE_USER = '" + djSettings.databaseUser + "'");
settings = settings.replaceFirst("DATABASE_PASSWORD = ''", "DATABASE_PASSWORD = '" + djSettings.databasePassword + "'");
}
if (settingsFile != null) {
settingsFile.setContents(new ByteArrayInputStream(settings.getBytes()), 0, monitor);
}
} catch (Exception e) {
Log.log(e);
RunInUiThread.async(() -> {
PyDialogHelpers.openCritical("Error creating django project", e.getMessage());
});
} finally {
monitor.done();
}
}
use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.
the class ModuleAdapter method getGlobalVariableNames.
public List<String> getGlobalVariableNames() {
List<String> globalNames = new ArrayList<String>();
if (this.sourceModule != null && nature != null) {
try {
ICodeCompletionASTManager astManager = nature.getAstManager();
if (astManager != null) {
TokensList tokens = astManager.getCompletionsForModule(this.sourceModule, new CompletionState(-1, -1, "", nature, ""));
for (IterTokenEntry entry : tokens) {
IToken token = entry.getToken();
globalNames.add(token.getRepresentation());
}
}
} catch (CompletionRecursionException e) {
Log.log(e);
}
} else {
for (SimpleAdapter adapter : getAssignedVariables()) {
globalNames.add(adapter.getName());
}
}
return globalNames;
}
use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.
the class ASTManager method loadFromFile.
public static ICodeCompletionASTManager loadFromFile(File astOutputFile) throws IOException {
ASTManager astManager = new ASTManager();
ProjectModulesManager projectModulesManager = new ProjectModulesManager();
ProjectModulesManager.loadFromFile(projectModulesManager, astOutputFile);
astManager.modulesManager = projectModulesManager;
return astManager;
}
use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.
the class PyCodeCompletionVisitor method visitChangedResource.
/**
* The code completion visitor is responsible for checking the changed resources in order to
* update the code completion cache for the project.
*
* This visitor just passes one resource and updates the code completion cache for it.
*/
@Override
public void visitChangedResource(IResource resource, ICallback0<IDocument> document, IProgressMonitor monitor) {
PythonNature pythonNature = getPythonNature(resource);
if (pythonNature != null) {
ICodeCompletionASTManager astManager = pythonNature.getAstManager();
if (astManager != null) {
IPath location = resource.getLocation();
astManager.rebuildModule(new File(location.toOSString()), document, resource.getProject(), new NullProgressMonitor(), pythonNature);
}
}
}
Aggregations