forked from eclipse-lemminx/lemminx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
noNamespaceSchemaLocation file missing CodeAction
A CodeAction for the error `schema-reference.4` that creates the file referenced in `xsi:noNamespaceSchemaLocation` if its missing. Closes eclipse-lemminx#691 Signed-off-by: David Thompson <davthomp@redhat.com>
- Loading branch information
1 parent
f8a0ab1
commit ce1a2ad
Showing
9 changed files
with
291 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...emminx/extensions/contentmodel/participants/codeactions/schema_reference_4CodeAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.extensions.contentmodel.participants.codeactions; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.eclipse.lemminx.commons.CodeActionFactory; | ||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.services.extensions.ICodeActionParticipant; | ||
import org.eclipse.lemminx.services.extensions.IComponentProvider; | ||
import org.eclipse.lemminx.settings.SharedSettings; | ||
import org.eclipse.lemminx.utils.StringUtils; | ||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.Range; | ||
|
||
/** | ||
* Code Action that creates a schema file referenced by | ||
* xsi:noNamespaceSchemaLocation if it is missing | ||
*/ | ||
public class schema_reference_4CodeAction implements ICodeActionParticipant { | ||
|
||
private static final Pattern PATH_FINDING_REGEX = Pattern.compile("[^']+'file:///(.+)',.*"); | ||
|
||
@Override | ||
public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument document, List<CodeAction> codeActions, | ||
SharedSettings sharedSettings, IComponentProvider componentProvider) { | ||
|
||
String missingFilePath = getPathFromDiagnostic(diagnostic); | ||
if (StringUtils.isEmpty(missingFilePath)) { | ||
return; | ||
} | ||
Path p = Paths.get(missingFilePath); | ||
if (p.toFile().exists()) { | ||
return; | ||
} | ||
|
||
// TODO: use the generator | ||
String schemaTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n\n</xs:schema>"; | ||
|
||
CodeAction makeSchemaFile = CodeActionFactory.createFile("Generate missing file '" + p.toFile().getName() + "'", | ||
"file:///" + missingFilePath, schemaTemplate, diagnostic); | ||
|
||
codeActions.add(makeSchemaFile); | ||
} | ||
|
||
/** | ||
* Extract the file to create from the diagnostic. Example diagnostic: | ||
* schema_reference.4: Failed to read schema document | ||
* 'file:///home/dthompson/Documents/TestFiles/potationCoordination.xsd', | ||
* because 1) could not find the document; 2) the document could not be read; 3) | ||
* the root element of the document is not <xsd:schema>. | ||
*/ | ||
private String getPathFromDiagnostic(Diagnostic diagnostic) { | ||
Matcher m = PATH_FINDING_REGEX.matcher(diagnostic.getMessage()); | ||
if (m.find()) { | ||
return m.group(1); | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/settings/XMLWorkspaceSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.settings; | ||
|
||
import org.eclipse.lsp4j.WorkspaceClientCapabilities; | ||
|
||
/** | ||
* A wrapper around LSP {@link WorkspaceClientCapabilities}. | ||
* | ||
*/ | ||
public class XMLWorkspaceSettings { | ||
|
||
private WorkspaceClientCapabilities workspace; | ||
|
||
public void setCapabilities(WorkspaceClientCapabilities workspace) { | ||
this.workspace = workspace; | ||
} | ||
|
||
/** | ||
* Returns true if the client can support the given resource operation kind and | ||
* false otherwise. | ||
* | ||
* @param kind the resource operation kind. | ||
* @return true if the client can support the given resource operation kind and | ||
* false otherwise. | ||
*/ | ||
public boolean isResourceOperationSupported(String kind) { | ||
return workspace != null && workspace.getWorkspaceEdit() != null | ||
&& workspace.getWorkspaceEdit().getResourceOperations() != null | ||
&& workspace.getWorkspaceEdit().getResourceOperations().contains(kind); | ||
} | ||
|
||
} |
Oops, something went wrong.