Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cf7179eb49f29dda82c939d1a9891eb2ee4adf1e (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
/*******************************************************************************
 * Copyright (c) 2010 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * CONTRIBUTORS:
 * 		Juergen Haug (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.core.ui.quickfix

import com.google.inject.Inject
import org.eclipse.emf.ecore.EClass
import org.eclipse.etrice.core.common.base.util.ImportHelpers
import org.eclipse.etrice.core.room.RoomModel
import org.eclipse.xtext.EcoreUtil2
import org.eclipse.xtext.resource.XtextResource
import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor
import org.eclipse.xtext.validation.Issue
import org.eclipse.etrice.core.room.RoomPackage

class RoomQuickFixProviderXtend extends RoomQuickfixProvider {
	
	@Inject ImportHelpers importHelpers
	
	// override xtext linking issues
	override getResolutionsForLinkingIssue(Issue issue) {			
		val xtextDocument = modificationContextFactory.createModificationContext(issue).getXtextDocument	
		
		val resolutions = xtextDocument?.readOnly([ resource |
			val issueString = xtextDocument.get(issue.offset, issue.length)	
			val acceptor = issueResolutionAcceptorProvider.get
			createLinkingQuickFixes(issue, issueString, resource, acceptor)
			acceptor.issueResolutions
		])
		
		return (resolutions + super.getResolutionsForLinkingIssue(issue)).toList
	}
	
	// dispatch own linking issues
	protected def void createLinkingQuickFixes(Issue issue, String issueString, XtextResource resource, IssueResolutionAcceptor acceptor) {
		val target = resource.getEObject(issue.uriToProblem.fragment)
		val reference = getUnresolvedEReference(issue, target)		
		
		if(reference !== null && RoomPackage.Literals.ROOM_CLASS.isSuperTypeOf(reference.EReferenceType)) {
			createLinkingImports(issue, issueString, resource, reference.EReferenceType, false, acceptor);
		}
	}
	
	protected def void createLinkingImports(Issue issue, String issueString, XtextResource resource, EClass type, boolean wildcard, IssueResolutionAcceptor acceptor) {
		// try first import based on model path
		val elementImports = importHelpers.createModelPathImports(issueString, resource, type, false)
		elementImports.forEach[imp |
			acceptor.accept(issue, '''Import '«imp.importedNamespace»' ''', '', null) [elem, ctx |
				val model = EcoreUtil2.getContainerOfType(elem, RoomModel)
				model.imports += imp
			]
		]
		// fallback: old style imports with uri
		if(elementImports.empty) {
			importHelpers.createURIImports(issueString, type, resource.URI).forEach[imp |
				acceptor.accept(issue, '''Import namespace '«imp.importedNamespace»' from '«imp.importURI»' ''', '', null) [elem, ctx |
					val model = EcoreUtil2.getContainerOfType(elem, RoomModel)
					model.imports += imp
				]
			]
		}
	}
	
}

Back to the top