Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disable broken handling of surrounding quotes pairs #613

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
*/
package org.eclipse.tm4e.languageconfiguration.internal;

import java.util.Objects;

import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
import org.eclipse.jface.text.source.ICharacterPairMatcher;
import org.eclipse.jface.text.source.ICharacterPairMatcherExtension;
import org.eclipse.tm4e.languageconfiguration.internal.model.AutoClosingPair;
import org.eclipse.tm4e.languageconfiguration.internal.registry.LanguageConfigurationRegistryManager;
import org.eclipse.tm4e.ui.internal.utils.ContentTypeHelper;
import org.eclipse.tm4e.ui.internal.utils.ContentTypeInfo;
Expand All @@ -28,6 +31,8 @@
public class LanguageConfigurationCharacterPairMatcher
implements ICharacterPairMatcher, ICharacterPairMatcherExtension {

private static DefaultCharacterPairMatcher NOOP_MATCHER = new DefaultCharacterPairMatcher(new char[0]);

@Nullable
private DefaultCharacterPairMatcher matcher;

Expand Down Expand Up @@ -111,25 +116,37 @@ private DefaultCharacterPairMatcher getMatcher(final IDocument document) {
this.document = document;

// initialize a DefaultCharacterPairMatcher by using character pairs of the language configuration.
final var sb = new StringBuilder();
final ContentTypeInfo info = ContentTypeHelper.findContentTypes(document);
final IContentType[] contentTypes = info == null ? null : info.getContentTypes();
if (contentTypes != null) {

if (contentTypes == null || contentTypes.length == 0) {
this.matcher = matcher = NOOP_MATCHER;
} else {
final var surroundingBracketsChars = new StringBuilder();
final var surroundingQuotesChars = new StringBuilder();
final var registry = LanguageConfigurationRegistryManager.getInstance();
for (final IContentType contentType : contentTypes) {
if (!registry.shouldSurroundingPairs(contentType)) {
continue;
}
final var surroundingPairs = registry.getSurroundingPairs(contentType);
for (final var surroundingPair : surroundingPairs) {
sb.append(surroundingPair.open);
sb.append(surroundingPair.close);
if (registry.shouldSurroundingPairs(contentType)) {
for (final AutoClosingPair surroundingPair : registry.getSurroundingPairs(contentType)) {
if (Objects.equals(surroundingPair.open, surroundingPair.close)) {
surroundingQuotesChars.append(surroundingPair.open);
} else {
surroundingBracketsChars.append(surroundingPair.open);
surroundingBracketsChars.append(surroundingPair.close);
}
}
}
}
if (surroundingBracketsChars.isEmpty() && surroundingQuotesChars.isEmpty()) {
this.matcher = matcher = NOOP_MATCHER;
} else {
final var bracketsChars = new char[surroundingBracketsChars.length()];
surroundingBracketsChars.getChars(0, surroundingBracketsChars.length(), bracketsChars, 0);
// TODO handle surroundingQuotesChars, DefaultCharacterPairMatcher cannot handle pairs correctly when open and close chars
// are identically, see https://github.com/eclipse/tm4e/issues/470
this.matcher = matcher = new DefaultCharacterPairMatcher(bracketsChars);
}
}
final var chars = new char[sb.length()];
sb.getChars(0, sb.length(), chars, 0);
this.matcher = matcher = new DefaultCharacterPairMatcher(chars);
}
return matcher;
}
Expand Down