Skip to content

Commit ed491d4

Browse files
authored
Merge pull request #502 from Coding/fix-language-mode-add-makefile
fix language mode, add makefile
2 parents cdbcf29 + 175e266 commit ed491d4

File tree

4 files changed

+106
-60
lines changed

4 files changed

+106
-60
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"id": "makefile",
3+
"aliases": ["makefile", "Makefile"],
4+
"extensions": [
5+
".make",
6+
".GNUmakefile",
7+
".makefile",
8+
".Makefile",
9+
".OCamlMakefile",
10+
".mak",
11+
".mk"
12+
]
13+
}

app/components/MonacoEditor/MonacoReact/grammars/languages.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ export default [
138138
mimeType: ['application/javascript'],
139139
aliases: ['JavaScript']
140140
},
141+
{
142+
id: 'makefile',
143+
aliases: ['makefile', 'Makefile'],
144+
extensions: ['.make', '.GNUmakefile', '.makefile', '.Makefile', '.OCamlMakefile', '.mak', '.mk'],
145+
filenames: ['Makefile', 'OCamlMakefile'],
146+
scopeName: 'source.makefile'
147+
},
141148
{
142149
id: 'json',
143150
scopeName: 'source.json',

app/components/MonacoEditor/state.js

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,34 @@ import uniqueId from 'lodash/uniqueId'
22
import React from 'react'
33
import { render } from 'react-dom'
44
import { observe, observable, computed, action, extendObservable, reaction } from 'mobx'
5-
65
import mime from 'mime-types'
76
import { Services } from 'monaco-languageclient'
87

9-
import codeEditorService from './codeEditorService'
108
import assignProps from 'utils/assignProps'
119
import getTabType from 'utils/getTabType'
1210
import is from 'utils/is'
11+
import config from 'config'
1312
import TabStore from 'components/Tab/store'
1413
import FileStore from 'commons/File/store'
1514
import EditorState from 'components/Editor/state'
1615
import { createMonacoServices } from 'components/MonacoEditor/Editors/createHelper'
17-
import { findLanguageByextensions, findModeByName } from './utils/findLanguage'
16+
import {
17+
findLanguageByextensions,
18+
findModeByName,
19+
findLanguageFileName
20+
} from './utils/findLanguage'
21+
import codeEditorService from './codeEditorService'
1822
import ConditionWidget from './ConditionWidget'
1923
import initialOptions from './monacoDefaultOptions'
2024
import config from 'config'
2125
import { state as virtualKeyState } from '../VirtualKey'
2226

23-
reaction(() => initialOptions.theme, (theme) => {
24-
monaco.editor.setTheme(theme)
25-
})
27+
reaction(
28+
() => initialOptions.theme,
29+
(theme) => {
30+
monaco.editor.setTheme(theme)
31+
}
32+
)
2633

2734
const state = observable({
2835
entities: observable.map({}),
@@ -31,7 +38,7 @@ const state = observable({
3138
activeMonacoEditor: null,
3239
editors: new Map(),
3340
activeEditorListeners: [],
34-
installed: false,
41+
installed: false
3542
})
3643

3744
const typeDetect = (title, types) => {
@@ -47,10 +54,9 @@ class EditorInfo {
4754
EditorState.entities.set(this.id, this)
4855
this.update(props)
4956
this.uri = this.filePath
50-
? (this.filePath.startsWith('jdt://')
51-
|| this.filePath.startsWith('omnisharp-metadata://')
52-
? this.filePath
53-
: `file://${config._ROOT_URI_}${this.filePath}`)
57+
? this.filePath.startsWith('jdt://') || this.filePath.startsWith('omnisharp-metadata://')
58+
? this.filePath
59+
: `file://${config._ROOT_URI_}${this.filePath}`
5460
: `inmemory://model/${this.id}`
5561

5662
if (!props.filePath || this.isMonaco) {
@@ -65,11 +71,14 @@ class EditorInfo {
6571
this.monacoElement.style.height = '100%'
6672

6773
if (this.filePath) {
68-
this.languageMode = findLanguageByextensions(this.filePath.split('.').pop()).id
74+
const fileName = this.filePath.split('/').pop()
75+
this.languageMode = fileName.includes('.')
76+
? findLanguageByextensions(fileName.split('.').pop()).id
77+
: findLanguageFileName(fileName).id
6978
}
7079
const model =
71-
monaco.editor.getModel(monaco.Uri.parse(this.uri).toString()) ||
72-
monaco.editor.createModel(this.content || '', this.languageMode, monaco.Uri.parse(this.uri))
80+
monaco.editor.getModel(monaco.Uri.parse(this.uri).toString()) ||
81+
monaco.editor.createModel(this.content || '', this.languageMode, monaco.Uri.parse(this.uri))
7382
this.uri = model.uri._formatted
7483

7584
this.model = model
@@ -88,7 +97,9 @@ class EditorInfo {
8897

8998
if (!state.installed) {
9099
// install Monaco language client services
91-
const services = createMonacoServices(monacoEditor, { rootUri: `file://${config._ROOT_URI_}` })
100+
const services = createMonacoServices(monacoEditor, {
101+
rootUri: `file://${config._ROOT_URI_}`
102+
})
92103
Services.install(services)
93104
state.installed = true
94105
}
@@ -110,13 +121,15 @@ class EditorInfo {
110121
}
111122
}
112123

113-
this.disposers.push(observe(this, 'content', (change) => {
114-
const content = change.newValue || ''
115-
if (content !== monacoEditor.getValue()) {
116-
this.startsWithUTF8BOM = this.content.charCodeAt(0) === 65279
117-
monacoEditor.setValue(content)
118-
}
119-
}))
124+
this.disposers.push(
125+
observe(this, 'content', (change) => {
126+
const content = change.newValue || ''
127+
if (content !== monacoEditor.getValue()) {
128+
this.startsWithUTF8BOM = this.content.charCodeAt(0) === 65279
129+
monacoEditor.setValue(content)
130+
}
131+
})
132+
)
120133

121134
if (this.content && this.content.length > 0) {
122135
this.startsWithUTF8BOM = this.content.charCodeAt(0) === 65279
@@ -139,10 +152,10 @@ class EditorInfo {
139152
monacoEditor.addAction({
140153
id: 'custom-comment',
141154
label: 'comment',
142-
keybindings: [
143-
monaco.KeyMod.CtrlCmd | monaco.KeyCode.US_SLASH
144-
],
145-
run: () => { /* no */ }
155+
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.US_SLASH],
156+
run: () => {
157+
/* no */
158+
}
146159
})
147160

148161
monacoEditor.onDidChangeCursorPosition((event) => {
@@ -159,7 +172,7 @@ class EditorInfo {
159172
if (props.selection) {
160173
const pos = {
161174
lineNumber: props.selection.startLineNumber,
162-
column: props.selection.startColumn,
175+
column: props.selection.startColumn
163176
}
164177
setTimeout(() => {
165178
monacoEditor.setSelection(props.selection)
@@ -188,7 +201,7 @@ class EditorInfo {
188201
const [lineNumber, column] = position.split(':')
189202
const pos = {
190203
lineNumber: Number(lineNumber),
191-
column: Number(column),
204+
column: Number(column)
192205
}
193206
this.monacoEditor.setPosition(pos)
194207
this.monacoEditor.revealPositionInCenter(pos, 1)
@@ -235,45 +248,43 @@ class EditorInfo {
235248
}
236249
}
237250

238-
setViewZoneForBreakPoint = (breakpoint) => {
239-
return new Promise((resolve, reject) => {
240-
const { path, line } = breakpoint
241-
242-
this.monacoEditor.changeViewZones((changeAccessor) => {
243-
const domNode = document.createElement('div')
244-
const viewZoneId = changeAccessor.addZone({
245-
afterLineNumber: line,
246-
heightInLines: 2,
247-
afterColumn: 0,
248-
domNode,
249-
})
250-
const handleCancel = () => {
251-
this.monacoEditor.changeViewZones((_changeAccessor) => {
252-
_changeAccessor.removeZone(viewZoneId)
253-
})
254-
}
255-
render(<ConditionWidget onChange={resolve} onCancel={handleCancel} breakpoint={breakpoint} />, domNode)
251+
setViewZoneForBreakPoint = breakpoint => new Promise((resolve, reject) => {
252+
const { path, line } = breakpoint
253+
254+
this.monacoEditor.changeViewZones((changeAccessor) => {
255+
const domNode = document.createElement('div')
256+
const viewZoneId = changeAccessor.addZone({
257+
afterLineNumber: line,
258+
heightInLines: 2,
259+
afterColumn: 0,
260+
domNode
256261
})
262+
const handleCancel = () => {
263+
this.monacoEditor.changeViewZones((_changeAccessor) => {
264+
_changeAccessor.removeZone(viewZoneId)
265+
})
266+
}
267+
render(
268+
<ConditionWidget onChange={resolve} onCancel={handleCancel} breakpoint={breakpoint} />,
269+
domNode
270+
)
257271
})
258-
}
272+
})
259273

260274
setDebuggerBreakPoint = (params) => {
261275
const { line, verified } = params
262276
const debuggerBreakPoint = this.debugBreakPoints.get(line)
263-
const newBreakPoint = this.monacoEditor.deltaDecorations(
264-
debuggerBreakPoint || [],
265-
[
266-
{
267-
range: new monaco.Range(line, 1, line, 1),
268-
options: {
269-
isWholeLine: false,
270-
glyphMarginClassName: verified
271-
? 'monaco-glyphMargin-breakpoint'
272-
: 'monaco-glyphMargin-breakpoint-unverified'
273-
}
277+
const newBreakPoint = this.monacoEditor.deltaDecorations(debuggerBreakPoint || [], [
278+
{
279+
range: new monaco.Range(line, 1, line, 1),
280+
options: {
281+
isWholeLine: false,
282+
glyphMarginClassName: verified
283+
? 'monaco-glyphMargin-breakpoint'
284+
: 'monaco-glyphMargin-breakpoint-unverified'
274285
}
275-
]
276-
)
286+
}
287+
])
277288
this.debugBreakPoints.set(line, newBreakPoint)
278289
}
279290

app/components/MonacoEditor/utils/findLanguage.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ function findLanguageByextensions (ext) {
1818
}
1919
return { id: 'plaintext' }
2020
}
21+
22+
function findLanguageFileName (fileName) {
23+
const monacoLanguages = monaco.languages.getLanguages()
24+
for (let i = 0; i < monacoLanguages.length; i++) {
25+
const info = monacoLanguages[i]
26+
if (info.filenames) {
27+
for (let j = 0; j < info.filenames.length; j++) {
28+
if (info.filenames[j] === fileName) return info
29+
}
30+
}
31+
}
32+
return { id: 'plaintext' }
33+
}
34+
2135
/**
2236
* 根据文件列表粗略判断当前项目语言,以启动相应的语言服务器
2337
* @param data 指定目录下文件列表
@@ -73,4 +87,5 @@ export {
7387
findLanguageByextensions,
7488
findModeByName,
7589
findLanguagesByFileList,
90+
findLanguageFileName,
7691
}

0 commit comments

Comments
 (0)