use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method renameDefinition.
private WorkspaceEdit renameDefinition(IDefinition definition, String newName) {
if (definition == null) {
if (languageClient != null) {
MessageParams message = new MessageParams();
message.setType(MessageType.Info);
message.setMessage("You cannot rename this element.");
languageClient.showMessage(message);
}
return new WorkspaceEdit(new HashMap<>());
}
WorkspaceEdit result = new WorkspaceEdit();
Map<String, List<TextEdit>> changes = new HashMap<>();
result.setChanges(changes);
if (definition.getContainingFilePath().endsWith(SWC_EXTENSION)) {
if (languageClient != null) {
MessageParams message = new MessageParams();
message.setType(MessageType.Info);
message.setMessage("You cannot rename an element defined in a SWC file.");
languageClient.showMessage(message);
}
return result;
}
if (definition instanceof IPackageDefinition) {
if (languageClient != null) {
MessageParams message = new MessageParams();
message.setType(MessageType.Info);
message.setMessage("You cannot rename a package.");
languageClient.showMessage(message);
}
return result;
}
IDefinition parentDefinition = definition.getParent();
if (parentDefinition != null && parentDefinition instanceof IPackageDefinition) {
if (languageClient != null) {
MessageParams message = new MessageParams();
message.setType(MessageType.Info);
message.setMessage("You cannot rename this element.");
languageClient.showMessage(message);
}
return result;
}
for (ICompilationUnit compilationUnit : compilationUnits) {
if (compilationUnit == null || compilationUnit instanceof SWCCompilationUnit) {
continue;
}
ArrayList<TextEdit> textEdits = new ArrayList<>();
if (compilationUnit.getAbsoluteFilename().endsWith(MXML_EXTENSION)) {
IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(compilationUnit.getAbsoluteFilename()));
IMXMLTagData rootTag = mxmlData.getRootTag();
if (rootTag != null) {
ArrayList<ISourceLocation> units = new ArrayList<>();
findMXMLUnits(mxmlData.getRootTag(), definition, units);
for (ISourceLocation otherUnit : units) {
TextEdit textEdit = new TextEdit();
textEdit.setNewText(newName);
Range range = LanguageServerUtils.getRangeFromSourceLocation(otherUnit);
if (range == null) {
continue;
}
textEdit.setRange(range);
textEdits.add(textEdit);
}
}
}
IASNode ast = null;
try {
ast = compilationUnit.getSyntaxTreeRequest().get().getAST();
} catch (Exception e) {
//safe to ignore
}
if (ast != null) {
ArrayList<IIdentifierNode> identifiers = new ArrayList<>();
findIdentifiers(ast, definition, identifiers);
for (IIdentifierNode identifierNode : identifiers) {
TextEdit textEdit = new TextEdit();
textEdit.setNewText(newName);
Range range = LanguageServerUtils.getRangeFromSourceLocation(identifierNode);
if (range == null) {
continue;
}
textEdit.setRange(range);
textEdits.add(textEdit);
}
}
if (textEdits.size() == 0) {
continue;
}
URI uri = Paths.get(compilationUnit.getAbsoluteFilename()).toUri();
changes.put(uri.toString(), textEdits);
}
return result;
}
use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method referencesForDefinition.
private void referencesForDefinition(IDefinition definition, List<Location> result) {
for (ICompilationUnit compilationUnit : compilationUnits) {
if (compilationUnit == null || compilationUnit instanceof SWCCompilationUnit) {
continue;
}
if (compilationUnit.getAbsoluteFilename().endsWith(MXML_EXTENSION)) {
IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(compilationUnit.getAbsoluteFilename()));
IMXMLTagData rootTag = mxmlData.getRootTag();
if (rootTag != null) {
ArrayList<ISourceLocation> units = new ArrayList<>();
findMXMLUnits(mxmlData.getRootTag(), definition, units);
for (ISourceLocation otherUnit : units) {
Location location = LanguageServerUtils.getLocationFromSourceLocation(otherUnit);
if (location == null) {
continue;
}
result.add(location);
}
}
}
IASNode ast;
try {
ast = compilationUnit.getSyntaxTreeRequest().get().getAST();
} catch (Exception e) {
continue;
}
ArrayList<IIdentifierNode> identifiers = new ArrayList<>();
findIdentifiers(ast, definition, identifiers);
for (IIdentifierNode otherNode : identifiers) {
Location location = LanguageServerUtils.getLocationFromSourceLocation(otherNode);
if (location == null) {
continue;
}
result.add(location);
}
}
}
use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method autoCompleteTypesForMXMLFromExistingTag.
/**
* Using an existing tag, that may already have a prefix or short name,
* populate the completion list.
*/
private void autoCompleteTypesForMXMLFromExistingTag(CompletionList result, IMXMLTagData offsetTag) {
IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(currentUnit.getAbsoluteFilename()));
String tagStartShortName = offsetTag.getShortName();
String tagPrefix = offsetTag.getPrefix();
PrefixMap prefixMap = mxmlData.getRootTagPrefixMap();
for (ICompilationUnit unit : compilationUnits) {
if (unit == null) {
continue;
}
Collection<IDefinition> definitions = null;
try {
definitions = unit.getFileScopeRequest().get().getExternallyVisibleDefinitions();
} catch (Exception e) {
//safe to ignore
continue;
}
for (IDefinition definition : definitions) {
if (!(definition instanceof ITypeDefinition)) {
continue;
}
ITypeDefinition typeDefinition = (ITypeDefinition) definition;
//or that the definition's base name matches the short name
if (tagStartShortName.length() == 0 || typeDefinition.getBaseName().startsWith(tagStartShortName)) {
//in a namespace with that prefix
if (tagPrefix.length() > 0) {
Collection<XMLName> tagNames = currentProject.getTagNamesForClass(typeDefinition.getQualifiedName());
for (XMLName tagName : tagNames) {
String tagNamespace = tagName.getXMLNamespace();
//not what we're using in this file
if (tagNamespace.equals(IMXMLLanguageConstants.NAMESPACE_MXML_2006)) {
//use the language namespace of the root tag instead
tagNamespace = mxmlData.getRootTag().getMXMLDialect().getLanguageNamespace();
}
String[] prefixes = prefixMap.getPrefixesForNamespace(tagNamespace);
for (String otherPrefix : prefixes) {
if (tagPrefix.equals(otherPrefix)) {
addDefinitionAutoCompleteMXML(typeDefinition, null, null, result);
}
}
}
} else {
//no prefix yet, so complete the definition with a prefix
MXMLNamespace ns = getMXMLNamespaceForTypeDefinition(typeDefinition, mxmlData);
addDefinitionAutoCompleteMXML(typeDefinition, ns.prefix, ns.uri, result);
}
}
}
}
}
use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method checkFilePathForAllProblems.
private boolean checkFilePathForAllProblems(Path path, Boolean quick) {
ICompilationUnit mainUnit = getCompilationUnit(path);
if (mainUnit == null) {
return false;
}
CompilerProject project = (CompilerProject) mainUnit.getProject();
Collection<ICompilerProblem> fatalProblems = project.getFatalProblems();
if (fatalProblems == null || fatalProblems.size() == 0) {
fatalProblems = project.getProblems();
}
if (fatalProblems != null && fatalProblems.size() > 0) {
URI uri = path.toUri();
PublishDiagnosticsParams publish = new PublishDiagnosticsParams();
publish.setDiagnostics(new ArrayList<>());
publish.setUri(uri.toString());
codeProblemTracker.trackFileWithProblems(uri);
for (ICompilerProblem problem : fatalProblems) {
addCompilerProblem(problem, publish);
}
codeProblemTracker.cleanUpStaleProblems();
if (languageClient != null) {
languageClient.publishDiagnostics(publish);
}
return true;
}
IASNode ast = null;
try {
ast = mainUnit.getSyntaxTreeRequest().get().getAST();
} catch (Exception e) {
System.err.println("Exception during build: " + e);
return false;
}
if (ast == null) {
return false;
}
Map<URI, PublishDiagnosticsParams> files = new HashMap<>();
try {
if (quick) {
PublishDiagnosticsParams params = checkCompilationUnitForAllProblems(mainUnit);
URI uri = Paths.get(mainUnit.getAbsoluteFilename()).toUri();
files.put(uri, params);
} else {
boolean continueCheckingForErrors = true;
while (continueCheckingForErrors) {
try {
for (ICompilationUnit unit : compilationUnits) {
if (unit == null || unit instanceof SWCCompilationUnit) {
//compiled compilation units won't have problems
continue;
}
PublishDiagnosticsParams params = checkCompilationUnitForAllProblems(unit);
URI uri = Paths.get(unit.getAbsoluteFilename()).toUri();
files.put(uri, params);
}
continueCheckingForErrors = false;
} catch (ConcurrentModificationException e) {
//when we finished building one of the compilation
//units, more were added to the collection, so we need
//to start over because we can't iterate over a modified
//collection.
}
}
//only clean up stale errors on a full check
codeProblemTracker.cleanUpStaleProblems();
}
} catch (Exception e) {
System.err.println("Exception during build: " + e);
e.printStackTrace();
return false;
}
if (languageClient != null) {
files.values().forEach(languageClient::publishDiagnostics);
}
return true;
}
use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method createCodeActionsForImport.
private void createCodeActionsForImport(Diagnostic diagnostic, List<Command> commands) {
String message = diagnostic.getMessage();
int start = message.lastIndexOf(" ") + 1;
int end = message.length() - 1;
String typeString = message.substring(start, end);
ArrayList<IDefinition> types = new ArrayList<>();
for (ICompilationUnit unit : compilationUnits) {
if (unit == null) {
continue;
}
try {
Collection<IDefinition> definitions = unit.getFileScopeRequest().get().getExternallyVisibleDefinitions();
if (definitions == null) {
continue;
}
for (IDefinition definition : definitions) {
if (definition instanceof ITypeDefinition) {
ITypeDefinition typeDefinition = (ITypeDefinition) definition;
String baseName = typeDefinition.getBaseName();
if (typeDefinition.getQualifiedName().equals(baseName)) {
//this definition is top-level. no import required.
continue;
}
if (baseName.equals(typeString)) {
types.add(typeDefinition);
}
}
}
} catch (Exception e) {
//safe to ignore
}
}
for (IDefinition definitionToImport : types) {
Command command = createImportCommand(definitionToImport);
if (command != null) {
commands.add(command);
}
}
}
Aggregations