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
Showing
3 changed files
with
106 additions
and
0 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
66 changes: 66 additions & 0 deletions
66
...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,66 @@ | ||
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.createDocumentWith( | ||
"Generate missing file '" + p.toFile().getName() + "'", | ||
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; | ||
} | ||
|
||
} |