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.
Provide basic experimental formatter which supports invalid XML
Fixes eclipse-lemminx#1195 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
6113791
commit 337fe5d
Showing
26 changed files
with
5,604 additions
and
823 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
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
...clipse/lemminx/extensions/contentmodel/participants/ContentModelFormatterParticipant.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) 2022 Red Hat, Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* 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.extensions.contentmodel.participants; | ||
|
||
import java.util.Collection; | ||
|
||
import org.eclipse.lemminx.dom.DOMElement; | ||
import org.eclipse.lemminx.extensions.contentmodel.model.CMDocument; | ||
import org.eclipse.lemminx.extensions.contentmodel.model.CMElementDeclaration; | ||
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager; | ||
import org.eclipse.lemminx.services.extensions.format.IFormatterParticipant; | ||
import org.eclipse.lemminx.services.format.FormatElementCategory; | ||
import org.eclipse.lemminx.services.format.XMLFormattingConstraints; | ||
import org.eclipse.lemminx.settings.SharedSettings; | ||
|
||
/** | ||
* Formatter participant which uses XSD/DTD grammar information to know the | ||
* {@link FormatElementCategory} of a given element. | ||
* | ||
* <p> | ||
* | ||
* This participant is enabled when 'xml.format.grammarAwareFormatting' setting | ||
* is set to true. | ||
* | ||
* </p> | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class ContentModelFormatterParticipant implements IFormatterParticipant { | ||
|
||
private static final String GRAMMAR_AWARE_FORMATTING = "grammarAwareFormatting"; | ||
|
||
private final ContentModelManager contentModelManager; | ||
|
||
public ContentModelFormatterParticipant(ContentModelManager contentModelManager) { | ||
this.contentModelManager = contentModelManager; | ||
} | ||
|
||
@Override | ||
public FormatElementCategory getFormatElementCategory(DOMElement element, | ||
XMLFormattingConstraints parentConstraints, SharedSettings sharedSettings) { | ||
Boolean enabled = sharedSettings.getFormattingSettings().getBoolean(GRAMMAR_AWARE_FORMATTING); | ||
if (enabled != null && !enabled) { | ||
return null; | ||
} | ||
|
||
Collection<CMDocument> cmDocuments = contentModelManager.findCMDocument(element); | ||
for (CMDocument cmDocument : cmDocuments) { | ||
CMElementDeclaration cmElement = cmDocument.findCMElement(element); | ||
if (cmElement != null) { | ||
if (cmElement.isStringType()) { | ||
return FormatElementCategory.PreserveSpace; | ||
} | ||
if (cmElement.isMixedContent()) { | ||
return FormatElementCategory.MixedContent; | ||
} | ||
} | ||
} | ||
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
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
Oops, something went wrong.