Skip to main content
summaryrefslogtreecommitdiffstats
blob: 42aed4317187fd45513d93b0fc27f33077e2a2c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package org.eclipse.fx.code.editor.ldef.langs.text;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.fx.code.editor.Input;
import org.eclipse.fx.code.editor.ldef.LDefStandaloneSetup;
import org.eclipse.fx.code.editor.ldef.lDef.LanguageDef;
import org.eclipse.fx.code.editor.ldef.lDef.Root;
import org.eclipse.fx.code.editor.ldef.text.LDefModelProvider;
import org.eclipse.fx.code.editor.services.URIProvider;
import org.osgi.service.component.annotations.Component;

@Component(service={LDefModelProvider.class,LDefDefaultModelProvider.class})
public class LDefDefaultModelProvider implements LDefModelProvider {
	private final Map<String,URI> definitionURI = new HashMap<>();
	private final Map<String,LanguageDef> definitionCache = new HashMap<>();
	private ResourceSetImpl rs;

	public LDefDefaultModelProvider() {
		LDefStandaloneSetup.doSetup();
		rs = new ResourceSetImpl();

		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/dart/dart.ldef"),"dart");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/js/js.ldef"),"js");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/java/java.ldef"),"java");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/xml/xml.ldef"),"xml");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/go/go.ldef"),"go");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/rust/rust.ldef"),"rust");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/swift/swift.ldef"),"swift");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/groovy/groovy.ldef"),"groovy");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/python/python.ldef"),"python");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/php/php.ldef"),"php");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/asciidoc/asciidoc.ldef"),"adoc");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/ceylon/ceylon.ldef"),"ceylon");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/kotlin/kotlin.ldef"),"kotlin");
		register(URI.createURI("platform:/plugin/org.eclipse.fx.code.editor.ldef.langs/org/eclipse/fx/code/editor/ldef/langs/lua/lua.ldef"),"lua");
	}

	private void register(URI uri, String fileSuffix) {
		definitionURI.put(fileSuffix, uri);
	}

	@Override
	public boolean applies(Input<?> input) {
		if( input instanceof URIProvider ) {
			String uri = ((URIProvider) input).getURI();
			String[] parts = uri.split("/");
			String lastSegment = parts[parts.length-1];
			int idx = lastSegment.lastIndexOf('.');
			if( idx != -1 ) {
				return definitionURI.containsKey(lastSegment.substring(idx+1));
			}
		}
		return false;
	}

	@Override
	public LanguageDef getModel(Input<?> input) {
		if( input instanceof URIProvider ) {
			String uri = ((URIProvider) input).getURI();
			String[] parts = uri.split("/");
			String lastSegment = parts[parts.length-1];
			int idx = lastSegment.lastIndexOf('.');
			if( idx != -1 ) {
				return getModelByExtension(lastSegment.substring(idx+1));
			}
		}
		return null;
	}

	public LanguageDef getModelByExtension(String extension) {
		LanguageDef def = definitionCache.get(extension);

		if( def == null ) {
			Resource resource = rs.getResource(definitionURI.get(extension), true);
			def = ((Root) resource.getContents().get(0)).getLanguageDefinition();
			definitionCache.put(extension, def);
		}

		return def;
	}
}

Back to the top