Skip to content

Commit 43facbe

Browse files
hqhhuangDobromir Hristov
andauthored
Wrap Declarations in Minimized and IDE mode (#646) rdar://106567892
* Wrap declarations in minimized and ide mode rdar://106567892 Wrap declarations in minimized and ide mode * break at any character for long identifiers break at any character for long identifiers * Only wrap when no smart wrapping Only wrap when no smart wrapping * Update logic for calculating if declaration has multiple lines Update logic for calculating if declaration has multiple lines * Use the correct formatted token Use the correct formatted token * make distinction bw have vs display multiple lines * adds a test for `has-multiple-lines` adds a test for `has-multiple-lines` * fix logic for window resize fix logic for window resize * add comment and clean up logic for `handleWindowResize` add comment and clean up logic for `handleWindowResize` * fix test fix test --------- Co-authored-by: Dobromir Hristov <[email protected]>
1 parent 09ba1fe commit 43facbe

File tree

15 files changed

+161
-54
lines changed

15 files changed

+161
-54
lines changed

src/components/DocumentationTopic.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,17 @@ export default {
731731
border-radius: var(--code-border-radius);
732732
}
733733
734+
/* wrap declaration only when not using smart wrapping */
735+
.source:not(.has-multiple-lines) > code {
736+
@include inTargetIde() {
737+
white-space: pre-wrap;
738+
739+
.token-identifier {
740+
word-break: break-all;
741+
}
742+
}
743+
}
744+
734745
.single-line {
735746
border-radius: var(--code-border-radius);
736747
}

src/components/DocumentationTopic/PrimaryContent/DeclarationGroup.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ export default {
7272
},
7373
},
7474
computed: {
75-
classes: ({ changeType, multipleLinesClass, hasMultipleLinesAfterAPIChanges }) => ({
75+
classes: ({ changeType, multipleLinesClass, displaysMultipleLinesAfterAPIChanges }) => ({
7676
[`declaration-group--changed declaration-group--${changeType}`]: changeType,
77-
[multipleLinesClass]: hasMultipleLinesAfterAPIChanges,
77+
[multipleLinesClass]: displaysMultipleLinesAfterAPIChanges,
7878
}),
7979
caption() {
8080
return this.declaration.platforms.join(', ');

src/components/DocumentationTopic/PrimaryContent/DeclarationSource.vue

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<pre
1313
ref="declarationGroup"
1414
class="source"
15-
:class="{ [multipleLinesClass]: hasMultipleLines }"
15+
:class="{ [multipleLinesClass]: displaysMultipleLines, 'has-multiple-lines': hasMultipleLines }"
1616
><CodeBlock ref="code"><Token
1717
v-for="(token, i) in formattedTokens"
1818
:key="i"
@@ -21,7 +21,7 @@
2121

2222
<script>
2323
import { indentDeclaration } from 'docc-render/utils/indentation';
24-
import { hasMultipleLines } from 'docc-render/utils/multipleLines';
24+
import { displaysMultipleLines } from 'docc-render/utils/multipleLines';
2525
import { multipleLinesClass } from 'docc-render/constants/multipleLines';
2626
import { getSetting } from 'docc-render/utils/theme-settings';
2727
import Language from 'docc-render/constants/Language';
@@ -36,7 +36,7 @@ export default {
3636
name: 'DeclarationSource',
3737
data() {
3838
return {
39-
hasMultipleLines: false,
39+
displaysMultipleLines: false,
4040
multipleLinesClass,
4141
};
4242
},
@@ -182,6 +182,14 @@ export default {
182182
183183
return newTokens;
184184
},
185+
hasMultipleLines({ formattedTokens }) {
186+
return formattedTokens.reduce((lineCount, tokens, idx) => {
187+
let REGEXP = /\n/g;
188+
if (idx === formattedTokens.length - 1) REGEXP = /\n(?!$)/g;
189+
if (!tokens.text) return lineCount; // handles TokenKind add & changed
190+
return lineCount + (tokens.text.match(REGEXP) || []).length;
191+
}, 1) >= 2;
192+
},
185193
},
186194
methods: {
187195
propsFor(token) {
@@ -192,13 +200,20 @@ export default {
192200
tokens: token.tokens,
193201
};
194202
},
203+
handleWindowResize() {
204+
this.displaysMultipleLines = displaysMultipleLines(this.$refs.declarationGroup);
205+
},
195206
},
196207
async mounted() {
208+
window.addEventListener('resize', this.handleWindowResize);
197209
if (this.language === Language.objectiveC.key.api) {
198210
await this.$nextTick();
199211
indentDeclaration(this.$refs.code.$el, this.language);
200212
}
201-
if (hasMultipleLines(this.$refs.declarationGroup)) this.hasMultipleLines = true;
213+
this.handleWindowResize();
214+
},
215+
beforeDestroy() {
216+
window.removeEventListener('resize', this.handleWindowResize);
202217
},
203218
};
204219
</script>
@@ -225,7 +240,7 @@ $docs-declaration-source-border-width: 1px !default;
225240
// the scrollbar is not clipped by this element depending on its border-radius
226241
@include new-stacking-context;
227242
228-
&.has-multiple-lines {
243+
&.displays-multiple-lines {
229244
border-radius: $border-radius;
230245
}
231246

src/components/DocumentationTopic/RelationshipsList.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ export default {
6868
};
6969
},
7070
computed: {
71-
classes({ changeType, multipleLinesClass, hasMultipleLinesAfterAPIChanges }) {
71+
classes({ changeType, multipleLinesClass, displaysMultipleLinesAfterAPIChanges }) {
7272
return [
7373
{
7474
inline: this.shouldDisplayInline,
7575
column: !this.shouldDisplayInline,
7676
[`changed changed-${changeType}`]: !!changeType,
77-
[multipleLinesClass]: hasMultipleLinesAfterAPIChanges,
77+
[multipleLinesClass]: displaysMultipleLinesAfterAPIChanges,
7878
},
7979
];
8080
},

src/components/DocumentationTopic/TopicsLinkBlock.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ export default {
148148
linkBlockClasses: ({
149149
changesClasses,
150150
hasAbstractElements,
151-
hasMultipleLinesAfterAPIChanges,
151+
displaysMultipleLinesAfterAPIChanges,
152152
multipleLinesClass,
153153
}) => ({
154154
'has-inline-element': !hasAbstractElements,
155-
[multipleLinesClass]: hasMultipleLinesAfterAPIChanges,
155+
[multipleLinesClass]: displaysMultipleLinesAfterAPIChanges,
156156
/*
157157
* The following display the "changes" highlight styles
158158
* on the parent div (this one) when there is not an abstract

src/constants/multipleLines.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* This source file is part of the Swift.org open source project
33
*
4-
* Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
* Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
* Licensed under Apache License v2.0 with Runtime Library Exception
66
*
77
* See https://swift.org/LICENSE.txt for license information
88
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

1111
// eslint-disable-next-line import/prefer-default-export
12-
export const multipleLinesClass = 'has-multiple-lines';
12+
export const multipleLinesClass = 'displays-multiple-lines';

src/mixins/apiChangesHelpers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import { ChangeNames } from 'docc-render/constants/Changes';
1212
import { multipleLinesClass } from 'docc-render/constants/multipleLines';
13-
import { hasMultipleLines } from 'docc-render/utils/multipleLines';
13+
import { displaysMultipleLines } from 'docc-render/utils/multipleLines';
1414

1515
const latestPrefix = 'latest_';
1616

@@ -28,10 +28,10 @@ export const APIChangesMultipleLines = {
2828
};
2929
},
3030
computed: {
31-
hasMultipleLinesAfterAPIChanges: ({ change, changeType, $refs }) => {
31+
displaysMultipleLinesAfterAPIChanges: ({ change, changeType, $refs }) => {
3232
if (!change && !changeType) return false;
3333

34-
return hasMultipleLines($refs.apiChangesDiff);
34+
return displaysMultipleLines($refs.apiChangesDiff);
3535
},
3636
},
3737
};

src/styles/base/_changes.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* This source file is part of the Swift.org open source project
33
*
4-
* Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
* Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
* Licensed under Apache License v2.0 with Runtime Library Exception
66
*
77
* See https://swift.org/LICENSE.txt for license information
@@ -19,7 +19,7 @@
1919
border-radius: $large-border-radius;
2020
position: relative;
2121

22-
&.has-multiple-lines, .has-multiple-lines & {
22+
&.displays-multiple-lines, .displays-multiple-lines & {
2323
border-radius: $border-radius;
2424
}
2525

src/utils/multipleLines.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* This source file is part of the Swift.org open source project
33
*
4-
* Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
* Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
* Licensed under Apache License v2.0 with Runtime Library Exception
66
*
77
* See https://swift.org/LICENSE.txt for license information
@@ -14,7 +14,7 @@
1414
* @returns {Boolean}
1515
*/
1616
// eslint-disable-next-line import/prefer-default-export
17-
export function hasMultipleLines(el) {
17+
export function displaysMultipleLines(el) {
1818
if (!el) return false;
1919

2020
const computedStyle = window.getComputedStyle(el.$el || el);

tests/unit/components/DocumentationTopic/PrimaryContent/DeclarationGroup.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ describe('DeclarationGroup', () => {
8080
expect(srcComponent.props('language')).toEqual('swift');
8181
});
8282

83-
it('applies the `multipleLinesClass` class if `hasMultipleLinesAfterAPIChanges` is true', () => {
83+
it('applies the `multipleLinesClass` class if `displaysMultipleLinesAfterAPIChanges` is true', () => {
8484
const wrapper = shallowMount({
8585
...DeclarationGroup,
8686
computed: {
8787
...DeclarationGroup.computed,
88-
hasMultipleLinesAfterAPIChanges: () => true,
88+
displaysMultipleLinesAfterAPIChanges: () => true,
8989
},
9090
},
9191
{

tests/unit/components/DocumentationTopic/PrimaryContent/DeclarationSource.spec.js

Lines changed: 92 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { shallowMount } from '@vue/test-utils';
1212
import DeclarationSource
1313
from 'docc-render/components/DocumentationTopic/PrimaryContent/DeclarationSource.vue';
1414
import { multipleLinesClass } from 'docc-render/constants/multipleLines';
15-
import { hasMultipleLines } from 'docc-render/utils/multipleLines';
15+
import { displaysMultipleLines } from 'docc-render/utils/multipleLines';
1616
import { themeSettingsState } from 'docc-render/utils/theme-settings';
1717
import { indentDeclaration } from 'docc-render/utils/indentation';
1818
import Language from '@/constants/Language';
@@ -24,7 +24,7 @@ const { TokenKind } = Token.constants;
2424
jest.mock('@/utils/indentation');
2525
jest.mock('@/utils/multipleLines');
2626

27-
hasMultipleLines.mockImplementation(() => false);
27+
displaysMultipleLines.mockImplementation(() => false);
2828

2929
describe('DeclarationSource', () => {
3030
let wrapper;
@@ -81,21 +81,102 @@ describe('DeclarationSource', () => {
8181
});
8282
});
8383

84-
it('applies the `multipleLinesClass` class if declaration group has multiple lines', async () => {
85-
expect(wrapper.vm.hasMultipleLines).toBe(false);
84+
it('applies the `multipleLinesClass` class if declaration group displays multiple lines', async () => {
85+
expect(wrapper.find({ ref: 'declarationGroup' }).classes()).not.toContain(multipleLinesClass);
8686

87-
hasMultipleLines.mockResolvedValue(true);
87+
displaysMultipleLines.mockResolvedValue(true);
8888
wrapper = shallowMount(DeclarationSource, { propsData });
89-
expect(wrapper.vm.hasMultipleLines).toBe(true);
90-
9189
await wrapper.vm.$nextTick();
9290
expect(wrapper.find({ ref: 'declarationGroup' }).classes()).toContain(multipleLinesClass);
9391
});
9492

95-
it('runs the hasMultipleLines, after `indentDeclaration` for ObjC code', async () => {
93+
it('applies the `has-multiple-lines` class if declaration group has multiple lines regardless of window width', async () => {
94+
const multiLineDeclaration = {
95+
tokens: [
96+
{
97+
kind: TokenKind.keyword,
98+
text: 'func',
99+
},
100+
{
101+
kind: TokenKind.text,
102+
text: ' ',
103+
},
104+
{
105+
kind: TokenKind.identifier,
106+
text: 'Foo',
107+
},
108+
{
109+
kind: TokenKind.text,
110+
text: '(\n',
111+
},
112+
{
113+
kind: TokenKind.externalParam,
114+
text: '_',
115+
},
116+
{
117+
kind: TokenKind.text,
118+
text: ' ',
119+
},
120+
{
121+
kind: TokenKind.internalParam,
122+
text: 'Bar',
123+
},
124+
{
125+
kind: TokenKind.text,
126+
text: ': ',
127+
},
128+
{
129+
kind: TokenKind.typeIdentifier,
130+
text: 'Self',
131+
},
132+
{
133+
kind: TokenKind.text,
134+
text: '.',
135+
},
136+
{
137+
kind: TokenKind.typeIdentifier,
138+
text: 'FooBar',
139+
},
140+
{
141+
kind: TokenKind.text,
142+
text: ',\n',
143+
},
144+
{
145+
kind: TokenKind.externalParam,
146+
text: 'context',
147+
},
148+
{
149+
kind: TokenKind.text,
150+
text: ': ',
151+
},
152+
{
153+
kind: TokenKind.typeIdentifier,
154+
text: 'Self',
155+
},
156+
{
157+
kind: TokenKind.text,
158+
text: '.',
159+
},
160+
{
161+
kind: TokenKind.typeIdentifier,
162+
text: 'Context',
163+
},
164+
{
165+
kind: TokenKind.text,
166+
text: ')',
167+
},
168+
],
169+
};
170+
171+
wrapper = shallowMount(DeclarationSource, { propsData: multiLineDeclaration });
172+
await wrapper.vm.$nextTick();
173+
expect(wrapper.find({ ref: 'declarationGroup' }).classes()).toContain('has-multiple-lines');
174+
});
175+
176+
it('runs the displaysMultipleLines, after `indentDeclaration` for ObjC code', async () => {
96177
const callStack = [];
97-
hasMultipleLines.mockImplementationOnce(() => {
98-
callStack.push('hasMultipleLines');
178+
displaysMultipleLines.mockImplementationOnce(() => {
179+
callStack.push('displaysMultipleLines');
99180
return true;
100181
});
101182
indentDeclaration.mockImplementationOnce(() => callStack.push('indentDeclaration'));
@@ -110,7 +191,7 @@ describe('DeclarationSource', () => {
110191
expect(indentDeclaration).toHaveBeenCalledTimes(1);
111192
expect(indentDeclaration)
112193
.toHaveBeenCalledWith(wrapper.find({ ref: 'code' }).vm.$el, Language.objectiveC.key.api);
113-
expect(callStack).toEqual(['indentDeclaration', 'hasMultipleLines']);
194+
expect(callStack).toEqual(['indentDeclaration', 'displaysMultipleLines']);
114195
});
115196
});
116197

tests/unit/components/DocumentationTopic/RelationshipsList.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ describe('RelationshipsList', () => {
8080
expect(wrapper.classes('inline')).toBe(false);
8181
});
8282

83-
it('applies the `multipleLinesClass` class if `hasMultipleLinesAfterAPIChanges` is true', () => {
83+
it('applies the `multipleLinesClass` class if `displaysMultipleLinesAfterAPIChanges` is true', () => {
8484
wrapper = shallowMount({
8585
...RelationshipsList,
8686
computed: {
8787
...RelationshipsList.computed,
88-
hasMultipleLinesAfterAPIChanges: () => true,
88+
displaysMultipleLinesAfterAPIChanges: () => true,
8989
},
9090
},
9191
{

tests/unit/components/DocumentationTopic/TopicsLinkBlock.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ describe('TopicsLinkBlock', () => {
250250
expect(wrapper.find(ContentNode).exists()).toBe(false);
251251
});
252252

253-
it('applies the `multipleLinesClass` class if `hasMultipleLinesAfterAPIChanges` is true', () => {
253+
it('applies the `multipleLinesClass` class if `displaysMultipleLinesAfterAPIChanges` is true', () => {
254254
wrapper = shallowMount({
255255
...TopicsLinkBlock,
256256
computed: {
257257
...TopicsLinkBlock.computed,
258-
hasMultipleLinesAfterAPIChanges: () => true,
258+
displaysMultipleLinesAfterAPIChanges: () => true,
259259
},
260260
},
261261
{

0 commit comments

Comments
 (0)