Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cc57244fca9a88d3887d24b4e03427193e22586d (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************
 * Copyright (c) 2000, 2015 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *     Red Hat Inc. - Modified from MakefileDocumentProvider for Automake
 *******************************************************************************/

package org.eclipse.cdt.internal.autotools.ui.editors.automake;

import java.io.IOException;
import java.util.Iterator;

import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;


public class AutomakeDocumentProvider extends TextFileDocumentProvider implements IMakefileDocumentProvider  {
	
	/**
	 * Remembers a IMakefile for each element.
	 */
	protected class AutomakefileFileInfo extends FileInfo {		
		public IMakefile fCopy;
	}
	
	@Override
	protected FileInfo createEmptyFileInfo() {
		return new AutomakefileFileInfo();
	}
	
	@Override
	protected FileInfo createFileInfo(Object element) throws CoreException {
		IMakefile original = null;
		if (element instanceof IFileEditorInput) {	
			IFileEditorInput input= (IFileEditorInput) element;
			if (input.getFile().exists())
				original= createMakefile(input.getFile().getLocation().toOSString());
		} else if (element instanceof IURIEditorInput) {
			IURIEditorInput input = (IURIEditorInput)element;
			original = createMakefile(input.getURI().getPath().toString());
		}
		if (original == null)
			return null;

		FileInfo info= super.createFileInfo(element);
		if (!(info instanceof AutomakefileFileInfo)) {
			return null;
		}

		AutomakefileFileInfo makefileInfo= (AutomakefileFileInfo) info;
		setUpSynchronization(makefileInfo);

		makefileInfo.fCopy = original;

		return makefileInfo;
	}
	
	/**
	 */
	private IMakefile createMakefile(String fileName) {
		IMakefile makefile = null;
		Automakefile automakefile = new Automakefile();
		try {
			automakefile.parse(fileName);
		} catch (IOException e) {
		}
		makefile = automakefile;
		return makefile;
	}
	
	@Override
	public IMakefile getWorkingCopy(Object element) {
		FileInfo fileInfo= getFileInfo(element);		
		if (fileInfo instanceof AutomakefileFileInfo) {
			return ((AutomakefileFileInfo) fileInfo).fCopy;
		}
		return null;
	}
	
	@Override
	public void shutdown() {
		Iterator<?> e = getConnectedElementsIterator();
		while (e.hasNext())
			disconnect(e.next());
	}
	
	@Override
	public void connect(Object element) throws CoreException {
		super.connect(element);
		IMakefile makefile = getWorkingCopy(element);
		AutomakeErrorHandler errorHandler = new AutomakeErrorHandler((IEditorInput)element);
		errorHandler.update(makefile);
	}
	
	@Override
	public IDocument getDocument(Object element) {
		FileInfo info= getFileInfo(element);
		if (info != null)
			return info.fTextFileBuffer.getDocument();
		return getParentProvider().getDocument(element);
	}
	
}

Back to the top